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

Java 如何读取整型、双精度和字符串并将其保存到变量?,java,string,int,double,java.util.scanner,Java,String,Int,Double,Java.util.scanner,对于我正在学习的一门在线课程,我试图在使用扫描仪读取变量(第二组)后,保存定义为变量的第二组整数、双精度和字符串。问题是我不知道如何处理我定义的第二组变量。我试着将它们实例化为一个新的变量,但我一直遇到错误。我需要帮助读取每个变量,然后保存它们 import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Sol

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

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

您可以使用赋值,而无需再次声明类型

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

您只需执行以下操作:

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

下面的代码将给出结果

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);   
如果s1字符串没有空格,也可以按照此操作

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

如果上一次读取的结尾和下一行的开头之间没有字符,则调用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();
}
}

请分享您看到的错误消息,以及您希望如何保存变量。您的IDE在int j=new j()处不显示错误吗;双y=新y();字符串k=新的k();?因为范围中已经定义了
int j
double y
String k
。@Apurva上的百分比非常高,所以不要使用IDE:(我认为您需要进一步了解如何在中声明和分配变量Java@PeterLawrey,对不起,先生。我错过了。虽然这个代码片段可以解决这个问题,但确实有助于提高您文章的质量。请记住,您是在为将来的读者回答这个问题,而这些人可能不知道您编写代码的原因建议。
            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);
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();
}
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();
}
}