Click here to Skip to main content
15,889,116 members
Home / Discussions / Java
   

Java

 
Questionjava program using oops concept Pin
coderyadav6-Aug-17 9:21
coderyadav6-Aug-17 9:21 
AnswerRe: java program using oops concept Pin
Richard MacCutchan6-Aug-17 21:42
mveRichard MacCutchan6-Aug-17 21:42 
AnswerRe: java program using oops concept Pin
Patrice T16-Aug-17 12:41
mvePatrice T16-Aug-17 12:41 
QuestionBluetooth low Energy Sensor Pin
Member 133472935-Aug-17 0:23
Member 133472935-Aug-17 0:23 
AnswerRe: Bluetooth low Energy Sensor Pin
Richard MacCutchan5-Aug-17 1:34
mveRichard MacCutchan5-Aug-17 1:34 
Questionwhy do we need serialization?if we dont do serialization what will happen.SCenarios please Pin
Prakashhhh2-Aug-17 7:35
Prakashhhh2-Aug-17 7:35 
AnswerRe: why do we need serialization?if we dont do serialization what will happen.SCenarios please Pin
Richard MacCutchan2-Aug-17 20:45
mveRichard MacCutchan2-Aug-17 20:45 
Question[Java] New to programming Pin
CoderKlip30-Jul-17 12:46
CoderKlip30-Jul-17 12:46 
Hey, I am new to the coding world as well as this site. I have been learning Java by myself for a few days now and wanted to make a gambling program just for fun and practice.

I am going to ask two questions, one short, and one long. I'll ask the long one first! How does my code look for a beginner? I know I haven't dipped into using multiple classes yet or anything, so how does this very basic program look?

package com.company;

import java.util.Scanner;
import java.util.Random;

public class StakingGame {

    static int cashStack = 500000;
    static int amountStaked;

    public static void main(String[] args) {
        System.out.println("Welcome to my staking game.");

        while(cashStack > 0) {
            amountStaked();
            System.out.println("Rolling...");

            whoWon();
        }
        System.out.println("You have no more money.");

    }

    public static void amountStaked() {
        Scanner scan = new Scanner(System.in);
        System.out.println("How much would you like to stake?");

        amountStaked = scan.nextInt();

        System.out.println("You have chosen to stake, " + amountStaked + "$, goodluck.");

    }

    public static int roll() {
        Random r = new Random();

        int min = 0;
        int max = 100;
        int rolled = r.nextInt(max-min) + min;

        return rolled;
    }

    public static void whoWon() {

        int playerRoll = roll();
        int computerRoll = roll();

        System.out.println("You have rolled a " + playerRoll);
        System.out.println("Your oppent has rolled a " + computerRoll);

        if(playerRoll > computerRoll) {
            stakeWon(0);
        } else if (playerRoll < computerRoll) {
            stakeLost(0);
        } else {
            System.out.println("You guys tied.");
        }
    }

    public static int stakeWon(int newAmount) {

        newAmount = cashStack + amountStaked;
        cashStack = newAmount;

        System.out.println("You have won the stake! You now have " + cashStack);
        return cashStack;
    }

    public static int stakeLost(int newAmount) {

        newAmount = cashStack - amountStaked;
        cashStack = newAmount;

        System.out.println("You have lost the stake.. You now have " + cashStack);
        return cashStack;
    }
}



My second question is, well, more of a question. I want this program to run until I either terminate it or the player runs out of money. So this is how I decided to go about that. I created this loop in my main method,

public static void main(String[] args) {
    System.out.println("Welcome to my staking game.");

    while(cashStack > 0) {
        amountStaked();
        System.out.println("Rolling...");

        whoWon();
    }
    System.out.println("You have no more money.");

}



Is that the best way to make the program run until you either lose or I terminate it??

Thankk you in advance!
AnswerRe: [Java] New to programming Pin
Richard MacCutchan30-Jul-17 20:40
mveRichard MacCutchan30-Jul-17 20:40 
AnswerRe: [Java] New to programming Pin
jschell1-Aug-17 8:00
jschell1-Aug-17 8:00 
AnswerRe: [Java] New to programming Pin
OM Bharatiya15-Aug-17 7:06
OM Bharatiya15-Aug-17 7:06 
Questionnot able to return the result to String method getLogin()? result should return "admin" or "user" as a String Pin
LOKENDRA YADAV15-Jul-17 9:46
LOKENDRA YADAV15-Jul-17 9:46 
AnswerRe: not able to return the result to String method getLogin()? result should return "admin" or "user" as a String Pin
Richard MacCutchan15-Jul-17 20:47
mveRichard MacCutchan15-Jul-17 20:47 
GeneralRe: not able to return the result to String method getLogin()? result should return "admin" or "user" as a String Pin
LOKENDRA YADAV15-Jul-17 22:28
LOKENDRA YADAV15-Jul-17 22:28 
GeneralRe: not able to return the result to String method getLogin()? result should return "admin" or "user" as a String Pin
Richard MacCutchan16-Jul-17 1:29
mveRichard MacCutchan16-Jul-17 1:29 
GeneralRe: not able to return the result to String method getLogin()? result should return "admin" or "user" as a String Pin
LOKENDRA YADAV16-Jul-17 2:42
LOKENDRA YADAV16-Jul-17 2:42 
GeneralRe: not able to return the result to String method getLogin()? result should return "admin" or "user" as a String Pin
Richard MacCutchan16-Jul-17 2:45
mveRichard MacCutchan16-Jul-17 2:45 
SuggestionRe: not able to return the result to String method getLogin()? result should return "admin" or "user" as a String Pin
Richard Deeming17-Jul-17 1:30
mveRichard Deeming17-Jul-17 1:30 
Questionvideo tracking Pin
Member 129924425-Jul-17 18:12
Member 129924425-Jul-17 18:12 
AnswerRe: video tracking Pin
Django_Untaken5-Jul-17 19:30
Django_Untaken5-Jul-17 19:30 
QuestionObserver Pattern - Restart Thread Again When Exception Occurs Pin
Django_Untaken5-Jul-17 8:52
Django_Untaken5-Jul-17 8:52 
AnswerRe: Observer Pattern - Restart Thread Again When Exception Occurs Pin
Richard MacCutchan5-Jul-17 21:55
mveRichard MacCutchan5-Jul-17 21:55 
QuestionNo value specified for parameter 2 Pin
karengsh3-Jul-17 5:27
karengsh3-Jul-17 5:27 
AnswerRe: No value specified for parameter 2 Pin
Richard Deeming3-Jul-17 5:29
mveRichard Deeming3-Jul-17 5:29 
GeneralRe: No value specified for parameter 2 Pin
karengsh3-Jul-17 5:33
karengsh3-Jul-17 5:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.