【问题标题】:Java Client-Server Double variable passing errorJava Client-Server Double 变量传递错误
【发布时间】:2016-10-21 00:18:51
【问题描述】:

如果我尝试将整数值从服务器传递回客户端并在屏幕上显示该值,则下面显示的代码有效。虽然当我将值类型更改为 Double 时,它​​会在 Client 类的以下行中抛出错误 Exception in thread "main" java.util.NoSuchElementException: 结果 = 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);

} }

【问题讨论】:

标签: java server double


【解决方案1】:

我检查了你的程序,它对于双精度和整数输入都可以正常工作。

【讨论】:

  • 当我输入值并回车时,它会抛出一个错误
  • 你能给出你失败的确切测试用例吗?
  • 操作:1 数量:10,56(我使用netbeans ide)。
  • 使用Netbeans IDE如下是输出选择操作:1金额:10.56 8.2368
  • 尝试清理项目。从 Run Menu -> Clean and Build 然后重新运行服务器和客户端
猜你喜欢
  • 1970-01-01
  • 2019-02-05
  • 2016-09-14
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多