2015-10-24 80 views
-2

我正在執行計算機猜測您的號碼的程序。並且它必須猜測一個隨機數。但是我怎麼讓隨機線低於或者高於num的數量呢?生成小於或大於「x」的隨機數字

package compguse; 
import java.util.*; 

public class Compguse { 


public static void main(String[] args) { 
    Scanner scan = new Scanner (System.in); 
    String a; 
    String b;  
    String c; 
    String ans; 
    String d;   
    int input =1; 
    System.out.println("do u have your number?"); 
    a = scan.nextLine(); 
    while (a.equalsIgnoreCase("yes")) 
    { 
    int ran = (int) Math.floor(Math.random()*100)+1; 
     System.out.println(" is" +ran +" your num?"); 
     a = scan.nextLine(); 
     if(a.equalsIgnoreCase("no")) 
     { 
      System.out.println("Was i too high or low?"); 
      b = scan.nextLine(); 
      if(b.equalsIgnoreCase("high")) 
      { 
      int ran1 = (int) Math.floor(Math.random() < (int) ran); 

      }  
     }  
    } 
} 

回答

1

只是做一個while循環,將實現該僞代碼:

例如,如果你希望它是小於猜測:

而隨機大於猜 隨機=新的隨機數

+0

如何把它放在代碼形式? – idk0namz