【问题标题】:How to read and save an Integer, Double, and String to a variable?如何读取 Integer、Double 和 String 并将其保存到变量中?
【发布时间】:2016-08-17 15:37:34
【问题描述】:

对于我正在参加的在线课程,在使用扫描仪读取它们(第二组)之后,我试图将我定义的第二组整数、双精度和字符串保存到变量中。问题是我不知道如何对我定义的第二组变量执行此操作。我已经尝试将它们实例化为一个新变量,但我一直遇到错误。我需要帮助来读取每个变量然后保存它们。

 import java.io.*;
 import java.util.*;
 import java.text.*;
 import java.math.*;
 import java.util.regex.*;

 public class Solution {

  public static void main(String[] args) {
    int i = 4;
    double d = 4.0;
    String s = "HackerRank ";


    int j = 4;
    double y = 9.0;
    String k = "is the best place to learn and practice coding!";


    int j = new j();
    double y = new y();
    String k = new k();
    j.scanner.nextInt();
    y.scanner.nextDouble();
    k.scanner.nextLine();


    System.out.print(j + i);
    System.out.print(d + y);
    System.out.print(s + k);

【问题讨论】:

  • 请分享您看到的错误信息以及您希望如何保存变量。
  • 您的 IDE 不会在 int j = new j(); 处显示错误吗?双 y = 新 y();字符串 k = 新 k();?因为int jdouble yString k已经在范围内定义
  • @Apurva 非常可怕,所以不要使用 IDE 的比例很高 :(
  • 我认为你需要了解更多关于如何在Java中声明和分配变量

标签: java string int double java.util.scanner


【解决方案1】:

你使用赋值而不再次声明类型。

int j = 4;
double y = 9.0;
String k = "is the best place to learn and practice coding!";

j = scanner.nextInt();
y = scanner.nextDouble();
k = scanner.nextLine();

【讨论】:

    【解决方案2】:

    如果在最后一次读取的结尾和下一行的开头之间没有字符,则调用 nextLine() 可能会返回一个空字符串。

    s1 = scan.nextLine();
    s1 = scan.nextLine();
    

    因此,当您阅读用户的输入时,请使用上述代码来完成该挑战。整个代码如下。

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
    public static void main(String[] args) {
    int i = 4;
    double d = 4.0;
    String s = "HackerRank ";
    
    Scanner scan = new Scanner(System.in);
    int i1;
    double d1;
    String s1;
    
    i1 = scan.nextInt();
    d1 = scan.nextDouble();
    s1 = scan.nextLine();
    s1 = scan.nextLine();
    
    System.out.println(i+i1);
    System.out.println(d+d1);
    System.out.println(s+s1);
    scan.close();
    }
    }
    

    【讨论】:

      【解决方案3】:
          int j = 12;
          double y = 4.0;
          String k = "is the best place to learn coding!";
      
      
          Scanner scanner = new Scanner( System.in );
      
          j = Integer.parseInt(scanner.nextLine());
          y = Double.parseDouble(scanner.nextLine());
          k = scanner.nextLine();
      
          //Ensure you print in new line
          System.out.print(j + i);
          System.out.print("\n");
          System.out.print(d + y);
          System.out.print("\n");
          System.out.print(s + k);
      

      【讨论】:

        【解决方案4】:

        您需要做的就是:

        Scanner scanner = new Scanner(System.in);//instantiate a Scanner object
        
        int j = scanner.nextInt();//Use the Scanner object to read an int value from the user
        double y = scanner.nextDouble();//Use the Scanner object to read an double value from the user
        String k = scanner.nextLine();//Use the Scanner object to read an line  from the user
        

        【讨论】:

          【解决方案5】:
               import java.io.*;
               import java.util.*;
               import java.text.*;
               import java.math.*;
               import java.util.regex.*;
          
               public class Solution {
          
                public static void main(String[] args) {
                  int i = 4;
                  double d = 4.0;
                  String s = "HackerRank ";
          
          
                  int j = 4;
                  double y = 9.0;
                  String k = "is the best place to learn and practice coding!";
          
          Scanner s = new Scanner(System.in);    
          
                  j = Integer.parseInt(s.nextLine());
                  y = Double.parseDouble(s.nextLine());
                  k = s.nextLine();
          
          
                  System.out.print(j + i);
                  System.out.print(d + y);
                  System.out.print(s + k);
          }
          }
          

          【讨论】:

          • @PeterLawrey,对不起先生。我错过了。
          【解决方案6】:
          int k = scan.nextInt();
          double l = scan.nextDouble();
          scan.nextLine();
          String f = scan.nextLine();
          System.out.println(k + i);
          System.out.println(l + d);
          System.out.println(s+f);
          

          【讨论】:

            【解决方案7】:

            下面的代码会给你结果

            int j;
            double y; 
            String k=null; 
            
            Scanner scan = new Scanner(System.in);
            j= scan.nextInt();
            y=scan.nextDouble();
            
            while(scan.hasNext()){
            k =scan.nextLine();
            }
            
            System.out.println(i+j);
            System.out.println(d+y);
            System.out.println(s+k);   
            

            【讨论】:

              【解决方案8】:
                          int i1= sc.nextInt();
                          double d1 = sc.nextDouble();
                          String s1 = sc.next();
                          System.out.println(i+i1);
                          System.out.println(d+d1);
                          System.out.println(s+s1);
              

              如果您的 s1 字符串没有空格,您也可以按照此操作。

              【讨论】:

                【解决方案9】:
                import java.io.*;
                import java.util.*;
                import java.text.*;
                import java.math.*;
                import java.util.regex.*;
                
                public class Solution {
                
                public static void main(String[] args) {
                    int i = 4;
                    double d = 4.0;
                    String s = "HackerRank ";
                
                    Scanner scan = new Scanner(System.in);
                
                
                    int j = 4;
                    double y = 9.0;
                    String k = "is the best place to learn and practice coding!";
                
                    Scanner f = new Scanner(System.in);    
                
                    j = Integer.parseInt(f.nextLine());
                    y = Double.parseDouble(f.nextLine());
                    k = f.nextLine();
                
                
                    System.out.println(j + i);
                    System.out.println(d + y);
                    System.out.println(s + k);
                
                
                           scan.close();
                }
                

                }

                【讨论】:

                • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
                猜你喜欢
                • 2021-12-27
                • 2021-06-02
                • 2020-08-17
                • 1970-01-01
                • 2019-11-20
                • 1970-01-01
                • 1970-01-01
                • 2019-01-31
                • 1970-01-01
                相关资源
                最近更新 更多