java 如何读取整数、双精度和字符串并将其保存到变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36820362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:54:05  来源:igfitidea点击:

How to read and save an Integer, Double, and String to a variable?

javastringintdoublejava.util.scanner

提问by dabberson567

For a online course I'm taking,I'm trying to save the 2nd set of integer, double, and string that I defined to variables, after reading them (the 2nd set) using a scanner. The problem is I don't know how to do that to the 2nd set of variables that I defined. I've tried instantiating them to a new variable,but I keep running into errors. I need help to read each variable and then save them.

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

 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);

回答by Peter Lawrey

You use assignment without declaring the type again.

您无需再次声明类型即可使用赋值。

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();

回答by Rishi Kumar J H

A call to nextLine() may return an empty string if there are no characters between the end of the last read and the beginning of the next line.

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

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

So to complete that challenge use the above code when you read the input from the user. And the entire code goes as follows.

因此,要完成该挑战,请在读取用户输入时使用上述代码。整个代码如下。

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();
}
}

回答by sui_generis

    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);

回答by Praveen Narwal

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);

回答by CdrAshw

The following code will give you the result

下面的代码会给你结果

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);   

回答by user6252820

            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);

you can also follow this if u don't have any spaces for your s1 string.

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

回答by Ridhi Vats

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();
}

}

}

回答by theVoid

All you need to do is the following:

您需要做的就是以下几点:

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

回答by Lakmal Vithanage

     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);
}
}