gpt4 book ai didi

Java switch case 始终运行默认代码

转载 作者:行者123 更新时间:2023-11-30 10:48:29 25 4
gpt4 key购买 nike

switch case 始终运行默认代码。我读了一些关于“换行”问题的文章,但我认为这里不是这种情况。因为我将第一个字符从“stringclient”字符串复制到一个字符变量中。但是当我在 Eclpise 中用我的 ATMServer 类运行它时,它工作得很好。只有当我从 cmd 执行它们时,才会出现此问题。所以有人知道发生了什么事吗?请帮忙。谢谢。

import java.io.*;
import java.net.*;

public class ATMClient {

private static final int PORT = 20000;

private static final char DRAW_STR = 'Α';
private static final char DEPOSIT_STR = 'Κ';
private static final char BALANCE_STR = 'Υ';
private static final char EXIT_STR = 'Ε';

private static boolean hasEnded = false;

public static void main(String args[]) throws IOException {
Socket dataSocket = new Socket("localhost",PORT);
InputStream is = dataSocket.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));

OutputStream os = dataSocket.getOutputStream();
PrintWriter out = new PrintWriter(os, true);


BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

String stringclient;

while(!hasEnded){
printMenu();

stringclient = input.readLine();

char optionCode = stringclient.charAt(0);
String tempData;
int amount;

switch(optionCode){
case EXIT_STR:
out.println(String.valueOf(EXIT_STR));
hasEnded = true;
continue;
case DRAW_STR:
tempData = stringclient.substring(1);

try{
amount = Integer.parseInt(tempData);
}catch(NumberFormatException e){
System.out.println("Το ποσό πρέπει να είναι αριθμός. Δοκιμάστε ξανά.");
System.out.println();
continue;
}

if(amount > 420){
System.out.println("Μπορείτε να κάνετε ανάληψη έως 420 ευρώ.");
System.out.println();
continue;
}

out.println(String.valueOf(DRAW_STR) + amount);
break;

case DEPOSIT_STR:
tempData = stringclient.substring(1);

try{
amount = Integer.parseInt(tempData);
}catch(NumberFormatException e){
System.out.println("Το ποσό πρέπει να είναι αριθμός. Δοκιμάστε ξανά.");
System.out.println();
continue;
}

out.println(String.valueOf(DEPOSIT_STR) + amount);

break;

case BALANCE_STR:
out.println(String.valueOf(BALANCE_STR));
break;

default:
System.out.println("Λάθος επιλογή. Δοκιμάστε ξανά.");
System.out.println();
continue;
}

String reply = in.readLine();

System.out.println(reply);
}

out.close();
os.close();
in.close();
is.close();
input.close();
dataSocket.close();
}
}

最佳答案

我觉得跟源文件的字符编码有关系。尝试使用“javac -encoding”进行编译。另外,我认为当您从 Eclipse 运行时,它会在您运行应用程序并输入您的值时处理字符编码,而对于 cmd,当您输入您的值时它使用您的默认系统编码,这就是存在这种不一致的原因;虽然只是一个猜测。

关于Java switch case 始终运行默认代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35784902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com