if else语句的问题......很重要

时间:2016-09-24 02:51:26

标签: java if-statement

抱歉,我的意思有点宽泛。但我在12年级学习计算机,我的任务是制作一个产生数字的小程序,如果它等于另一组数字,你就赢了,有点像彩票。我发现很难让随机数生成器工作两次,所以我预先加载了一个数字变量。现在我的最后一个任务是让它说“你赢”,如果其中一个数字匹配,如果没有“你松散”,基本上我只是不能匹配变量?`

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); }


    `

我已经得到了一点点但是我的“胜利”似乎无法解决变量,这是我的新代码。

 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");
}`

2 个答案:

答案 0 :(得分:0)

  

现在我的最后一项任务是让它说“你赢”,如果其中一个数字匹配,如果不是“你松了”

这样的事情:

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");
    }

答案 1 :(得分:0)

代码的问题在于,只有经过很长时间才能赢得成功的彩票。尝试为它做一些算法,以增加获胜的可能性。

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");
    }
}