每当我输入高位数的整数时,它就会起作用;当我输入一个字符时,验证会起作用,因为它会显示错误消息。
但是,我不能为低号码做同样的事情吗?
我不确定我在做什么错。
这是完整的代码
package client;
import static com.sun.org.apache.xerces.internal.util.FeatureState.is;
import static com.sun.org.apache.xerces.internal.util.PropertyState.is;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static int Highnumber;
public static int Lownumber;
public static void main(String[] args) {
String hostName = "localhost";
int hostPort = 4444;
if (args.length != 0) {
if (args[0] != null) {
hostName = args[0];
}
if (args[1] != null) {
hostPort = Integer.parseInt(args[1]);
}
}
System.out.println("Trying to Connect to Server");
try (Socket serverSocket = new Socket(hostName, hostPort);
DataOutputStream os = new DataOutputStream(new BufferedOutputStream(serverSocket.getOutputStream()));
BufferedReader is = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()))) {
Scanner ScannerInput = new Scanner(System.in);
System.out.println("Enter High Number -");
Integer highnumber = null;
while (highnumber == null) {
try {
highnumber = Integer.parseInt(ScannerInput.next());
} catch (NumberFormatException e) {
System.out.println("ERROR, ENTER AN INTEGER:");
}
}
Integer lownumber = null;
System.out.println("Enter low Number -");
while (lownumber == null) {
try {
Integer number = Integer.parseInt(ScannerInput.next());
if (number <= highnumber) {
lownumber = number;
}else{
System.out.println("ERROR,needs to be lower");
}
} catch (NumberFormatException e) {
System.out.println("ERROR, ENTER AN INTEGER:");
}
}
Highnumber=highnumber;
Lownumber=lownumber ;
在以下部分中获取错误:
}
int numberToFind = Integer.parseInt(is.readLine());
System.out.println("Target Number Set" + "\nStart Guessing!:");
int Tries = 0;
int UserInput = 0;
while (UserInput != numberToFind) {
UserInput = ScannerInput.nextInt();
os.writeInt(UserInput);
os.flush();
System.out.println(is.readLine());
Tries++;
}
System.out.println("Well Done! You found the number " + numberToFind + " in " + Tries + " tries");
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
答案 0 :(得分:0)
好的,我做了一些重组,不必在循环时分层
//Scanner scannerInput = new Scanner(System.in);
System.out.println("Enter High Number -");
Integer highnumber = null;
while (highnumber == null) {
try {
highnumber = Integer.parseInt(clientInput.next());
} catch (NumberFormatException e) {
System.out.println("ERROR, ENTER AN INTEGER:");
}
}
Integer lownumber = null;
System.out.println("Enter low Number -");
while (lownumber == null) {
try {
Integer number = Integer.parseInt(clientInput.next());
if (number <= highnumber) {
lownumber = number;
}else{
System.out.println("ERROR,needs to be lower");
}
} catch (NumberFormatException e) {
System.out.println("ERROR, ENTER AN INTEGER:");
}
}
Highnumber=highnumber;
Lownumber=lownumber ;
}
答案 1 :(得分:-1)
根据您的要求,我开发了带有注释的示例代码,您将了解程序背后的逻辑。
int higer_number = 0;
int lower_number;
Scanner scan = new Scanner(System.in);
/**
* putting this section into try catch,because if you get characters or some
* other input this will throw exception.
*/
try {
/**
* Entering high number as input
*/
System.out.print("Enter High Number :");
higer_number = scan.nextInt();
/**
* Entering low number as input
*/
System.out.print("Enter Lower Number :");
lower_number = scan.nextInt();
/**
* Checking is low value is lower then high value?
*/
if (lower_number >= higer_number) {
System.out.println("PLease Enter Low Value Then Higer Number.");
}
} catch (Exception e) {
System.out.println("Please Enter Valid Integer");
}
scan.close();