2015-11-12 54 views
0

import java.util.Random; import java.util.Scanner;设置哨兵值java代码

公共类ArithmeticGame {

static int right = 0; 
static int wrong = 0; 
static int total = 0; 
static int per = 0; 

public static void main(String[] args) { 

    Scanner s = new Scanner(System.in); 
    Random num = new Random(); 
    Random num1 = new Random(); 
    int r = 0; 

    while (r != 999) { 
     total++; 
     per = (right/total) * 100; 

     int a = num.nextInt(20); 
     int b = num1.nextInt(20); 

     int sum = a + b; 
     System.out.println(" \n          ARITHETIC GAME!"); 
     System.out.println("What is the sum of " + a + " + " + b + " ?"); 
     r = s.nextInt(); 

     if (r == sum) { 
      System.out.println("Your answer is correct."); 
      right++; 
     } 

     if (r != sum) { 
      System.out.println("OOPS! Your answer is Wrong"); 
      wrong++; 
     } 
     System.out.println("So far you have " + right + " right answers " + wrong + " wrong answers out of total " 
       + total + " questions"); 
    } 
} 

} 我需要设置标记值,这样,当我进入999计划应当终止和响应999不应该对最终值数。

+0

你需要解释什么错误 – fdsa

回答

1

更改环是

while(true) { 
.... 
} 

行之后接着R = s.nextInt();但检查它是否是对还是错之前,添加:

if(r == 999) { 
    break; 
}