我只是在Java中搞乱了一点,我创建了一个小程序。一切都工作正常,直到最后一步,整数bankValue总是保持在1000,当它应该根据你输或赢而改变。如果有人可以解释可能导致这种混乱的原因并且可能会给我一些关于我的代码的反馈,那么会很高兴。
以下是源代码:http://ideone.com/Kl1FsB
//Dice game created 2015-08-29
import java.util.Scanner;
import java.util.Random;
public class cantSleep {
//----------How the program should work-------------------//
//Create a dice program which lets the user choose between h/l (High/Low)
//The dice is then rolled and depending on which number it lands on there are different
//actions.
// l = 1-3, h = 4-6
//The user should start with a bank value of a 1000 marks.
//For each win the amount of the bet should be doubled.
//After a win/loss the user should be prompted again, asking if they want to play again.
//----------How to create the program --------------------//
//Create a boolean variable for the loop so the user can be prompted after one run.
//Create one integer for the bank value and one for the dice.
//Create a char and set make it so the Scanner takes in values for either 'h' or 'l'
//If the user gives any value other than the ones above prompt them to do it over again.
//Create a random method which random's out the dice number.
//Create an switch/case for the different outcomes on the dice number.
//------Problems encountered----//
//Everything works until the last step, whenever you win/lose
//the bankValue doesn't change.
//Random number generator method.
public static int randomGenerator(int startValue, int lastValue)
{
Random rand = new Random();
int randomNum = rand.nextInt((lastValue-startValue) + 1) + startValue;
return randomNum;
}
public static void diceWin(int bankValue, int bet, int dice, char choice, boolean runAgain)
{
Scanner userInput = new Scanner(System.in);
bankValue += bet;
System.out.println("Congratulations, the dice shows " + dice + " and you just won yourself " + bet + " $.");
System.out.print("Would you like to bet again? (y/n): ");
choice = userInput.nextLine().charAt(0);
while(choice != 'y' && choice != 'n')
{
System.out.println("Please use the characters 'y' or 'n' next time.");
System.out.print("Would you like to bet " + bet + "? (y/n): ");
choice = userInput.next().charAt(0);
if(choice == 'n')
{
runAgain = false;
}
}
}
public static void diceLoss(int bankValue, int bet, int dice, char choice, boolean runAgain)
{
Scanner userInput = new Scanner(System.in);
bankValue -= bet;
System.out.println("Sorry, the dice shows " + dice + " and you just lost " + bet + " $.");
System.out.print("Would you like to bet again? (y/n): ");
choice = userInput.nextLine().charAt(0);
while(choice != 'y' && choice != 'n')
{
System.out.println("Please use the characters 'y' or 'n' next time.");
System.out.print("Would you like to bet " + bet + "? (y/n): ");
choice = userInput.next().charAt(0);
if(choice == 'n')
{
runAgain = false;
}
}
}
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
//Variable declaration
boolean runAgain = true, low = true;
int bankValue = 1000, dice = 0, bet = 0;
char choice;
//Start of the program
System.out.println("Hello and welcome to the dice machine.");
while(runAgain == true)
{
System.out.println("Your current balance is: " + bankValue + " $.");
//User input, if the bet amount is bigger than the balance it gives an error message out.
System.out.print("How much would you like to bet?: ");
bet = userInput.nextInt();
while(bet > bankValue)
{
System.out.print("You do not have that much money on your bank account, please try again: ");
bet = userInput.nextInt();
}
//Prompts the user for a verification of the bet.
//If anything but 'y' and 'n' is typed in it gives an error message out.
//If the user types a 'y' in the program continues to run, if they type an 'n' in the program closes.
System.out.print("Do you really want to bet " + bet + "? (y/n): ");
choice = userInput.next().charAt(0);
while(choice != 'y' && choice != 'n')
{
System.out.println("Please use the characters 'y' or 'n' next time.");
System.out.print("Would you like to bet " + bet + "? (y/n): ");
choice = userInput.next().charAt(0);
if(choice == 'n')
{
System.exit(0);
}
}
//Asks the user what he/she would like to put the bet on. (h/l)
System.out.print("What would you like to put " + bet + " on, 'h' or 'l'? (l = 1-3, h = 4-6): ");
choice = userInput.next().charAt(0);
while(choice != 'h' && choice != 'l')
{
System.out.println("Please use the characters 'h' or 'l' next time.");
System.out.print("What would you like to put " + bet + " on, 'h' or 'l'? (l = 1-3, h = 4-6): ");
choice = userInput.next().charAt(0);
}
//Executing the randomGenerator method.
dice = randomGenerator(1, 6);
//Giving the dice numbers boolean values.
switch(dice)
{
case 1:
low = true;
break;
case 2:
low = true;
break;
case 3:
low = true;
break;
case 4:
low = false;
break;
case 5:
low = false;
break;
case 6:
low = false;
break;
default:
System.out.println("Error, dice gave the number " + dice);
}
if((low == true && choice == 'l') || (low == false && choice == 'h'))
{
diceWin(bankValue, bet, dice, choice, runAgain);
} else if((low == true && choice == 'h') || (low == false && choice == 'l')) {
diceLoss(bankValue, bet, dice, choice, runAgain);
} else {
System.out.println("Something went horribly wrong, please check the source code.");
}
}
userInput.close();
}
}
答案 0 :(得分:1)
首先请注意Java是pass by value。
因此,在{u'DestinationCountries': [
{u'CountryName': u'Antigua And Barbuda', u'CountryCode': u'AG'},
{u'CountryName': u'Argentina', u'CountryCode': u'AR'},
{u'CountryName': u'Armenia', u'CountryCode': u'AM'},
{u'CountryName': u'Aruba', u'CountryCode': u'AW'},
...
}
或diceLoss
方法中,diceWin
的值不会反映在您的实际变量中。您需要从方法中返回值并将其存储为或,您可以将bankValue
声明为类级变量,并且不要在方法中传递它。
例如:
bankValue
答案 1 :(得分:0)
您的bankValue
仅限于main
。你可能会传递参考资料。在当前的程序中,java不会在diceWin
和diceLoss
中修改该值,您在输入或输入中修饰的变量与在bankValue
中声明的main
完全不同
您可以做的是,将变量移出主要部分。哪个是Class级别然后修改它。
//Dice game created by Kemal 'kemkoi' Kojic 2015-08-29
import java.util.Scanner;
import java.util.Random;
public class cantSleep {
// ----------How the program should work-------------------//
// Create a dice program which lets the user choose between h/l (High/Low)
// The dice is then rolled and depending on which number it lands on there
// are different
// actions.
// l = 1-3, h = 4-6
// The user should start with a bank value of a 1000 marks.
// For each win the amount of the bet should be doubled.
// After a win/loss the user should be prompted again, asking if they want
// to play again.
// ----------How to create the program --------------------//
// Create a boolean variable for the loop so the user can be prompted after
// one run.
// Create one integer for the bank value and one for the dice.
// Create a char and set make it so the Scanner takes in values for either
// 'h' or 'l'
// If the user gives any value other than the ones above prompt them to do
// it over again.
// Create a random method which random's out the dice number.
// Create an switch/case for the different outcomes on the dice number.
// ------Problems encountered----//
// Everything works until the last step, whenever you win/lose
// the bankValue doesn't change.
private static int bankValue = 1000;
// Random number generator method.
public static int randomGenerator(int startValue, int lastValue) {
Random rand = new Random();
int randomNum = rand.nextInt((lastValue - startValue) + 1) + startValue;
return randomNum;
}
public static void diceWin(int bet, int dice, char choice, boolean runAgain) {
Scanner userInput = new Scanner(System.in);
bankValue += bet;
System.out.println("Congratulations, the dice shows " + dice
+ " and you just won yourself " + bet + " $.");
System.out.print("Would you like to bet again? (y/n): ");
choice = userInput.nextLine().charAt(0);
while (choice != 'y' && choice != 'n') {
System.out
.println("Please use the characters 'y' or 'n' next time.");
System.out.print("Would you like to bet " + bet + "? (y/n): ");
choice = userInput.next().charAt(0);
if (choice == 'n') {
runAgain = false;
}
}
}
public static void diceLoss(int bet, int dice, char choice, boolean runAgain) {
Scanner userInput = new Scanner(System.in);
bankValue -= bet;
System.out.println("Sorry, the dice shows " + dice
+ " and you just lost " + bet + " $.");
System.out.print("Would you like to bet again? (y/n): ");
choice = userInput.nextLine().charAt(0);
while (choice != 'y' && choice != 'n') {
System.out
.println("Please use the characters 'y' or 'n' next time.");
System.out.print("Would you like to bet " + bet + "? (y/n): ");
choice = userInput.next().charAt(0);
if (choice == 'n') {
runAgain = false;
}
}
}
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
// Variable declaration
boolean runAgain = true, low = true;
int dice = 0, bet = 0;
char choice;
// Start of the program
System.out.println("Hello and welcome to the dice machine.");
while (runAgain == true) {
System.out.println("Your current balance is: " + bankValue + " $.");
// User input, if the bet amount is bigger than the balance it gives
// an error message out.
System.out.print("How much would you like to bet?: ");
bet = userInput.nextInt();
while (bet > bankValue) {
System.out
.print("You do not have that much money on your bank account, please try again: ");
bet = userInput.nextInt();
}
// Prompts the user for a verification of the bet.
// If anything but 'y' and 'n' is typed in it gives an error message
// out.
// If the user types a 'y' in the program continues to run, if they
// type an 'n' in the program closes.
System.out.print("Do you really want to bet " + bet + "? (y/n): ");
choice = userInput.next().charAt(0);
while (choice != 'y' && choice != 'n') {
System.out
.println("Please use the characters 'y' or 'n' next time.");
System.out.print("Would you like to bet " + bet + "? (y/n): ");
choice = userInput.next().charAt(0);
if (choice == 'n') {
System.exit(0);
}
}
// Asks the user what he/she would like to put the bet on. (h/l)
System.out.print("What would you like to put " + bet
+ " on, 'h' or 'l'? (l = 1-3, h = 4-6): ");
choice = userInput.next().charAt(0);
while (choice != 'h' && choice != 'l') {
System.out
.println("Please use the characters 'h' or 'l' next time.");
System.out.print("What would you like to put " + bet
+ " on, 'h' or 'l'? (l = 1-3, h = 4-6): ");
choice = userInput.next().charAt(0);
}
// Executing the randomGenerator method.
dice = randomGenerator(1, 6);
// Giving the dice numbers boolean values.
switch (dice) {
case 1:
low = true;
break;
case 2:
low = true;
break;
case 3:
low = true;
break;
case 4:
low = false;
break;
case 5:
low = false;
break;
case 6:
low = false;
break;
default:
System.out.println("Error, dice gave the number " + dice);
}
if ((low == true && choice == 'l')
|| (low == false && choice == 'h')) {
diceWin(bet, dice, choice, runAgain);
} else if ((low == true && choice == 'h')
|| (low == false && choice == 'l')) {
diceLoss(bet, dice, choice, runAgain);
} else {
System.out
.println("Something went horribly wrong, please check the source code.");
}
}
userInput.close();
}
}
但是在可变对象的情况下你是正确的。生成和修改新副本的规则仅适用于原语和不可变类。
例如:
仔细查看以下程序
public class Main {
public static void main(String[] args) {
Person pe = new Person();
pe.setName("so");
int i=5;
changeSomething(pe,i);
System.out.println(pe.getName());// changeSo
System.out.println(i);// 5
}
private static void changeSomething(Person pe, int i) {
pe.setName("changeSo");i=10;
}
}
答案 2 :(得分:0)
将int
变量传递给函数时,该函数适用于该变量的副本;你可以让函数返回它的新值,并将函数返回的值赋给变量。