[英]Java Client-Server Double variable passing error
Code shown below works, if I am trying to pass Integer value from the server back to the client and display the value on screen. 如果我尝试将Integer值从服务器传递回客户端并在屏幕上显示该值,则下面显示的代码有效。 Although when I change the value type to Double, it throws error Exception in thread "main" java.util.NoSuchElementException in following line in Client class: result = sc1.nextDouble();
尽管当我将值类型更改为Double时,它在Client类的以下行中的线程“ main” java.util.NoSuchElementException中引发错误Exception :result = sc1.nextDouble();
class Client {
public static void main(String args[]) throws IOException {
int operation;
Double amount;
Double result;
Scanner sc = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
Socket s = new Socket ("127.0.0.1",1234);
Scanner sc1 = new Scanner (s.getInputStream());
System.out.println("1. EUR → GBP");
System.out.println("2. EUR → USD");
System.out.println("3. EUR → MKD");
System.out.println("\n");
System.out.println("Chose operation :");
operation = sc.nextInt();
System.out.println("Amount :");
amount=sc2.nextDouble();
PrintStream p = new PrintStream(s.getOutputStream());
p.println(amount);
p.println(operation);
result = sc1.nextDouble();
System.out.println(result);
}
}
__ __
class Server {
public static void main(String args[]) throws IOException {
int operation;
Double amount;
Double result = 1.00;
ServerSocket s1 = new ServerSocket(1234);
Socket ss = s1.accept();
Scanner sc = new Scanner(ss.getInputStream());
amount = sc.nextDouble();
operation = sc.nextInt();
if(operation == 1) //EUR-GBP
{
result = amount * 0.78;
}
if(operation == 2) //EUR-USD
{
result = amount* 1.13;
}
if(operation == 3) //EUR-MKD
{
result = amount* 61.18;
}
PrintStream p = new PrintStream(ss.getOutputStream());
p.println(result);
} } }}
我已经检查了您的程序,它对于双精度和整数输入工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.