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问题吗?