2015-09-05 74 views
0

这是我到目前为止有:如何更改用户输入(Java)的位置?

import java.util.Scanner; 
public class CurrencyConverter 
{ 
    public static void main(String [ ] args) 
    {   
     Scanner pesos = new Scanner(System.in); 
     System.out.println("What is the current exchange rate for pesos to dollars?"); 

     System.out.println(); 
     Scanner yen = new Scanner(System.in); 
     System.out.println("What is the current exchange rate for yen to dollars?"); 

     System.out.println(); 
     Scanner euros = new Scanner(System.in); 
     System.out.println("What is the current exchange rate for euros to dollars?"); 
    } 
} 

什么我添加到改变从

enter image description here

enter image description here

感谢。

+0

使用一台'Scanner'并使用'Scanner#nextLine'在用户需要时读取响应 – MadProgrammer

回答

0

不要创建三个变量Scanner。只需使用其中一个并使用input.nextLine();,其中input它是Scanner类型的变量。然后你可以将它存储在三个String变量中。

import java.util.Scanner; 
public class CurrencyConverter 
{ 
    public static void main(String [ ] args) 
    {   
     Scanner input = new Scanner(System.in); 

     String pesos; 
     System.out.println("What is the current exchange rate for pesos to dollars?"); 
     pesos = input.nextLine(); 

     String yen; 
     System.out.println("What is the current exchange rate for yen to dollars?"); 
     yen = input.nextLine(); 

     String euros; 
     System.out.println("What is the current exchange rate for euros to dollars?"); 
     euros = input.nextLine(); 
    } 
}