当前位置:编程学习 > JAVA >>

Java缂栫▼锛氬綋鐢ㄦ埛杈撳叆涓嶆槸鏁板瓧鏃跺浣曞惊鐜姹傝緭鍏ユ暟瀛楋紵 - 宸茶В鍐?- 鎼滄悳闂棶

import java.util.Scanner;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double i = Math.random() * 99 + 1;// Math.random()浼氳嚜鍔ㄤ骇鐢熶竴涓?.0-1.0鐨勫弻绮惧害闅忔満鏁?br>int iRandom = (int) i;
System.out.println(iRandom);
int iEnter;
System.out.println("鐚滄暟瀛楁父鎴忋€俓n绯荤粺宸查€夊畾涓€涓?鈥?00涔嬮棿鐨勬暣鏁帮紝璇风寽娴嬪畠锛?);
Scanner scanner = new Scanner(System.in);
do {
iEnter = scanner.nextInt();
if (iEnter < 1 || iEnter > 100) {
System.out.println("杈撳叆瓒婄晫锛岃杈撳叆1-100涔嬮棿鐨勬暣鏁帮細");
continue;
}
if (iEnter < iRandom) {
System.out.println("鐚滃皬浜嗭紝璇风户缁寽娴嬶細");
}
if (iEnter > iRandom) {
System.out.println("鐚滃ぇ浜嗭紝璇风户缁寽娴嬶細");
}
} while (iEnter != iRandom);
System.out.println("Great!You guess the right number.");
}

}


涓婇潰鏄€滅寽鏁板瓧娓告垙鈥濄€?/p>

do while寰幆杈撳叆鏁板瓧鐭ラ亾鐚滄祴姝g‘銆?/p>

閭e鏋滆緭鍏ョ殑涓嶆槸鏁板瓧锛屽浣曟彁绀洪敊璇紝骞惰姹傞噸鏂拌緭鍏ュ憿锛?/p>

鎴戝紕浜嗗崐澶╋紝瀹炲湪寮勪笉鍑烘潵

楂樻墜甯垜涓嬩笅

答案:鍛靛懙锛岀寽鏁板瓧鎴戝C璇█鐨勬椂鍊欎篃鍐欒繃銆傝嚜宸卞啓涓垽鏂嚱鏁帮紝鐒跺悗鐢ㄤ釜while寰幆涓€鐩村垽鏂槸鍚︽槸鏁板瓧鍗冲彲銆?/p>

import java.util.Scanner;

public class Test {


//鐢↗AVA鑷甫鐨勫嚱鏁?
public static boolean isNumeric(String str){
  for (int i = str.length();--i>=0;){  
   if (!Character.isDigit(str.charAt(i))){
    return false;
   }
  }
  return true;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double i = Math.random() * 99 + 1;// Math.random()浼氳嚜鍔ㄤ骇鐢熶竴涓?.0-1.0鐨勫弻绮惧害闅忔満鏁?br>int iRandom = (int) i;
System.out.println(iRandom);
int iEnter;
System.out.println("鐚滄暟瀛楁父鎴忋€俓n绯荤粺宸查€夊畾涓€涓?鈥?00涔嬮棿鐨勬暣鏁帮紝璇风寽娴嬪畠锛?);
String str;
Scanner scanner = new Scanner(System.in);

do {

while(true){
    str= String.valueOf(scanner.next());
 if(!Test.isNumeric(str))
    System.out.println("璇疯緭鍏ヤ竴涓暟瀛?");
 else
    break;
}

iEnter = Integer.parseInt(str);
if (iEnter < 1 || iEnter > 100) {
System.out.println("杈撳叆瓒婄晫锛岃杈撳叆1-100涔嬮棿鐨勬暣鏁帮細");
continue;
}
if (iEnter < iRandom) {
System.out.println("鐚滃皬浜嗭紝璇风户缁寽娴嬶細");
}
if (iEnter > iRandom) {
System.out.println("鐚滃ぇ浜嗭紝璇风户缁寽娴嬶細");
}
} while (iEnter != iRandom);
System.out.println("Great!You guess the right number.");
}

}

鍏跺疄杩欎釜婊$畝鍗曠殑

绠€鍗曠殑鍋氭硶灏辨槸灏哠canner scanner = new Scanner(System.in)鏀惧湪do閲岄潰

鍙笉杩囪矊浼艰鍏堝皢iEnter鍒濆鍖?/p>

鍏蜂綋鏀规硶鐪嬩笅闈細

import java.util.Scanner;

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  double i = Math.random() * 99 + 1;// Math.random()浼氳嚜鍔ㄤ骇鐢熶竴涓?.0-1.0鐨勫弻绮惧害闅忔満鏁?br>  int iRandom = (int) i;
  int iEnter = 0;//灏唅Enter鍒濆鍖?br>  System.out.println("鐚滄暟瀛楁父鎴忋€俓n绯荤粺宸查€夊畾涓€涓?鈥?00涔嬮棿鐨勬暣鏁帮紝璇风寽娴嬪畠锛?);
  do {
   Scanner scanner = new Scanner(System.in);//杩欏彞浠庡師鍏堢殑澶栭潰鏀鹃噷闈?/p>

   //涓嬮潰if璇彞鍒ゆ柇鏄惁閿欒杈撳叆
   if (!scanner.hasNextInt()) {
    System.out.println("杈撳叆閿欒锛岃杈撳叆鏁存暟锛?);
    continue;
   }
   iEnter = scanner.nextInt();
   if (iEnter < 1 || iEnter > 100) {
    System.out.println("杈撳叆瓒婄晫锛岃杈撳叆1-100涔嬮棿鐨勬暣鏁帮細");
    continue;
   }
   if (iEnter < iRandom) {
    System.out.println("鐚滃皬浜嗭紝璇风户缁寽娴嬶細");
   }
   if (iEnter > iRandom) {
    System.out.println("鐚滃ぇ浜嗭紝璇风户缁寽娴嬶細");
   }
  } while (iEnter != iRandom);
  System.out.println("Great!You guess the right number.");
 }
}

100鍒?鎴戠粰浣犲紕

鍥犱负濡傛灉涓嶆槸鏁板瓧iEnter = scanner.nextInt();杩欎釜浼氬嚭閿?/p>

鎵€浠ョ敤 try catch鎹曡幏寮傚父锛屽苟鍦╟atch鍧楅噷闈㈡姏鍑洪敊璇彁绀鸿緭鍏ユ暟瀛?/p>

try{

//鍔犲叆鎹曡幏鐨勮鍙?/p>

}catch(//鎶涘嚭閿欒淇℃伅锛?/p>

//閲嶆柊杈撳叆

}

 

上一个:JAVA中用Set集合的TreeSet类对姓名和编号排序
下一个:有人能给我提点简单的JAVA问题吗?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,