mardi 25 juillet 2023

Java Game with While Loop as sum 2 randoms

I'm doing a program in java that needs me have the 2 random in 6 to sum and also use a while loop. This is all I currently have, can anyone help as what I'm missing?

import java.util.Random;//*
import java.io.IOException;//*
class Test
{
    public static void main(String[] args) throws IOException{ //*
        System.out.print("按下Enter開始遊戲");
        int point=getPoint();
        int sum=0;      
        
        while(true)
        {
            System.in.read();
            
            if(System.in.read()=='n')
            {
                System.out.println("遊戲結束");
                break;
            }
            if(sum==0)
            {
                switch(point)
                    {
                            case 7:
                            case 11:
                                
                                    System.out.println("總和:"+point+" Winner");

                                break;                  
                            case 2:
                            case 3:
                            case 12:
                                
                                    System.out.println("總和:"+point+" False");                   
                                                    
                                break;
                            default:                                
                                break;
                    }       
                                
            }
                                
                if(sum==point)
                {
                    System.out.println("第1次總和為:"+point+"目前總和為"+sum+" Winner");
                    break;                      
                }else
                {                                                               
                    System.out.println("第1次總和為:"+point+"目前總和為"+sum+"是否繼續?");
                                        
                }
                sum=getPoint();                                                             
            
        }
    }
    public static int getPoint()
    {
        Random Ran =new Random();           
        int a=Ran.nextInt(6)+1;
        int b=Ran.nextInt(6)+1;//math.random改寫
        int sum=a+b;
        return sum; 
    }
}

...

else
                {                                                               
                    System.out.println("第1次總和為:"+point+"目前總和為"+sum+"是否繼續?");
                                        
                }

Why does it will print twice when I just Enter ''a'' 1 times in ''While'' Loop

I want when I Enter ''a'' each times and This code just run 1 times not 2 times




Aucun commentaire:

Enregistrer un commentaire