簡體   English   中英

Java客戶端-服務器雙變量傳遞錯誤

[英]Java Client-Server Double variable passing error

如果我嘗試將Integer值從服務器傳遞回客戶端並在屏幕上顯示該值,則下面顯示的代碼有效。 盡管當我將值類型更改為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM