if else 语句有问题...重要
trouble with if else statement... important
抱歉,我的标题有点宽泛。但我在 12 年级学习计算机,我的任务是制作一个生成数字的小程序,如果它等于另一组数字,你就赢了,有点像彩票。我发现很难让随机数生成器工作两次,所以我预加载了一个数字变量。现在我的最后一个任务是让它说 "you win" 如果其中一个数字匹配,如果不匹配 "you loose",基本上我只是不能匹配变量?`
Random rn = new Random();
String answer = " 3\n 12 \n 27";
for(int i =0; i < 3; i++)
{
int answer1 = rn.nextInt(30) + 1;
delay(2000);
System.out.println(answer1);
}
System.out.println("And the winning numbers are...");
delay(2000);
System.out.println(answer); }
`
我走得更远了一点,但我的 'win' 似乎无法解析为变量,这是我的新代码`
Random rn = new Random();
String answer = " 3\n 12 \n 27";
for(int i =0; i < 3; i++)
{
int answer1 = rn.nextInt(30) + 1;
System.out.println(answer1);
System.out.println("And the winning numbers are...");
System.out.println(answer);
if (answer1 == 3)
if (answer1 == 12)
if (answer1 == 27)
win = true;
if (win) {
System.out.println("You win!");
} else
System.out.println("You loose");
}`
now my last task is to get it to say "you win" if one of the numbers match, and if not "you loose"
像这样:
public static void main(String[] args) throws InterruptedException {
Random rn = new Random();
int preloadedAnswer = 19;
boolean win = false;
for (int i = 0; i < 3; i++) {
int answer1 = rn.nextInt(30) + 1;
if (answer1 == preloadedAnswer)
win = true;
}
if (win) {
System.out.println("You win!");
} else
System.out.println("You loose");
}
该代码的问题是您需要很长时间才能中奖。尝试为它做一些算法来增加获胜的可能性。
import java.io.*;
import java.util.Random;
public class Lottery{
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Random rn = new Random();
int ch = 1;
while(ch == 1){
String str = "";
String str1 = "";
String str2 = "";
for(int i = 0; i < 3; i++){
int answer1 = rn.nextInt(9) + 1;
//delay(2000);
str = str + "-" + answer1;
str1=str1+answer1;
}
System.out.println(str);
str = "";
for(int i =0; i < 3; i++){
int answer1 = rn.nextInt(9) + 1;
//delay(2000);
str = str + "-" + answer1;
str2 = str2 + answer1;
}
System.out.println(str);
if(Integer.parseInt(str1) == Integer.parseInt(str2)){
System.out.println("Crongrats !! You are the Lucky Winner !!!!");
}
else
System.out.println("Better Luck Next Time");
System.out.println("Wanna Try Again ? 1=YES, 0=NO");
ch = Integer.parseInt(br.readLine());
}
System.out.println("Thanks for Playing Lottery with us !! Come back soon");
}
}
抱歉,我的标题有点宽泛。但我在 12 年级学习计算机,我的任务是制作一个生成数字的小程序,如果它等于另一组数字,你就赢了,有点像彩票。我发现很难让随机数生成器工作两次,所以我预加载了一个数字变量。现在我的最后一个任务是让它说 "you win" 如果其中一个数字匹配,如果不匹配 "you loose",基本上我只是不能匹配变量?`
Random rn = new Random();
String answer = " 3\n 12 \n 27";
for(int i =0; i < 3; i++)
{
int answer1 = rn.nextInt(30) + 1;
delay(2000);
System.out.println(answer1);
}
System.out.println("And the winning numbers are...");
delay(2000);
System.out.println(answer); }
`
我走得更远了一点,但我的 'win' 似乎无法解析为变量,这是我的新代码`
Random rn = new Random();
String answer = " 3\n 12 \n 27";
for(int i =0; i < 3; i++)
{
int answer1 = rn.nextInt(30) + 1;
System.out.println(answer1);
System.out.println("And the winning numbers are...");
System.out.println(answer);
if (answer1 == 3)
if (answer1 == 12)
if (answer1 == 27)
win = true;
if (win) {
System.out.println("You win!");
} else
System.out.println("You loose");
}`
now my last task is to get it to say "you win" if one of the numbers match, and if not "you loose"
像这样:
public static void main(String[] args) throws InterruptedException {
Random rn = new Random();
int preloadedAnswer = 19;
boolean win = false;
for (int i = 0; i < 3; i++) {
int answer1 = rn.nextInt(30) + 1;
if (answer1 == preloadedAnswer)
win = true;
}
if (win) {
System.out.println("You win!");
} else
System.out.println("You loose");
}
该代码的问题是您需要很长时间才能中奖。尝试为它做一些算法来增加获胜的可能性。
import java.io.*;
import java.util.Random;
public class Lottery{
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Random rn = new Random();
int ch = 1;
while(ch == 1){
String str = "";
String str1 = "";
String str2 = "";
for(int i = 0; i < 3; i++){
int answer1 = rn.nextInt(9) + 1;
//delay(2000);
str = str + "-" + answer1;
str1=str1+answer1;
}
System.out.println(str);
str = "";
for(int i =0; i < 3; i++){
int answer1 = rn.nextInt(9) + 1;
//delay(2000);
str = str + "-" + answer1;
str2 = str2 + answer1;
}
System.out.println(str);
if(Integer.parseInt(str1) == Integer.parseInt(str2)){
System.out.println("Crongrats !! You are the Lucky Winner !!!!");
}
else
System.out.println("Better Luck Next Time");
System.out.println("Wanna Try Again ? 1=YES, 0=NO");
ch = Integer.parseInt(br.readLine());
}
System.out.println("Thanks for Playing Lottery with us !! Come back soon");
}
}