mind taking a look at this code? it gives me an error and I cant figure out what to do. Its for a class and any help would be appreciated. I have to make a lottery program with three digit numbers randomly generated individualy and then the user can win in a number of combinations. Refer to the code for a clarification.
介意看看这段代码?它给了我一个错误,我无法弄清楚该怎么做。它的课程和任何帮助将不胜感激。我必须制作一个单独随机生成的三位数字的彩票计划,然后用户可以赢得多种组合。请参阅代码以获得说明。
import java.util.Scanner;
public class Lottery
{
public static void main(String[] args)
{
//declare and initialized variables and objects
Scanner in = new Scanner(System.in);
String lotteryNum = "";
String userGuess = "";
for(int tries = 0; tries >= 0; tries++)
{
int randomNum1 = (int)(Math.random() * 10);
int randomNum2 = (int)(Math.random() * 10);
int randomNum3 = (int)(Math.random() * 10);
int computerNum1 = getNumericValue (randomNum1.charAt(0) );
int computerNum2 = getNumericValue (randomNum2.charAt(1) );
int computerNum3 = getNumericValue (randomNum3.charAt(2) );
String randomNum = "" + randomNum1 + randomNum2 + randomNum3;
System.out.print("Please enter three numbers:");
int num1 = in.nextInt();
int num2 = in.nextInt();
int num3 = in.nextInt();
if (num1 == computerNum1 && num2 == computerNum2)
{
System.out.println("You win!");
}
else if (num2 == computerNum2 && num3 == computerNum3)
{
System.out.println("You Win!");
}
else if (num1 == computerNum1 && num2 ==computerNum2 && num3 == computerNum3)
{
System.out.println("Sorry, You Lost");
}
}}}
2 个解决方案
#1
4
This won't work: randomNum1.charAt(0)
这不起作用:randomNum1.charAt(0)
An int is not a String and not even an object, and so you can't call methods on it. It's a primitive and like all primitives has no methods. It just is.
int不是String,甚至不是对象,因此您无法在其上调用方法。它是一个原始的,就像所有原语都没有方法一样。它只是。
Instead I suppose that you could use int division and mod operators to get what you want, but to be able to tell for sure, you should post your actual assignment instructions because your description of your ultimate goal is not clear to me.
相反,我认为你可以使用int division和mod运算符来获得你想要的东西,但是为了能够确定,你应该发布你的实际作业指令,因为你对你的最终目标的描述并不清楚。
If all you want are 3 random numbers, 0 to 9, then just use the numbers that you already have, and no need to try to manipulate them further.
如果你想要的只是3个随机数,0到9,那么只需使用你已有的数字,而不需要尝试进一步操作它们。
#2
0
You can't call charAt
on an int
, because it's not a string. Try:
你不能在int上调用charAt,因为它不是字符串。尝试:
String.valueOf(randomNum1).charAt(0)
#1
4
This won't work: randomNum1.charAt(0)
这不起作用:randomNum1.charAt(0)
An int is not a String and not even an object, and so you can't call methods on it. It's a primitive and like all primitives has no methods. It just is.
int不是String,甚至不是对象,因此您无法在其上调用方法。它是一个原始的,就像所有原语都没有方法一样。它只是。
Instead I suppose that you could use int division and mod operators to get what you want, but to be able to tell for sure, you should post your actual assignment instructions because your description of your ultimate goal is not clear to me.
相反,我认为你可以使用int division和mod运算符来获得你想要的东西,但是为了能够确定,你应该发布你的实际作业指令,因为你对你的最终目标的描述并不清楚。
If all you want are 3 random numbers, 0 to 9, then just use the numbers that you already have, and no need to try to manipulate them further.
如果你想要的只是3个随机数,0到9,那么只需使用你已有的数字,而不需要尝试进一步操作它们。
#2
0
You can't call charAt
on an int
, because it's not a string. Try:
你不能在int上调用charAt,因为它不是字符串。尝试:
String.valueOf(randomNum1).charAt(0)