如何读取并保存变量的整数,双精度和字符串?

时间:2016-04-24 07:19:51

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

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

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

9 个答案:

答案 0 :(得分:3)

您可以在不再声明类型的情况下使用作业。

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

答案 1 :(得分:2)

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

答案 2 :(得分: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 :(得分:0)

您需要做的就是:

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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

答案 6 :(得分:0)

以下代码将为您提供结果

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

答案 7 :(得分:0)

import bcrypt

# at creation first:
password = u"seCr3t"
hashed_password = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())

# first attempt:
password = u"seCrEt"
bcrypt.checkpw(password.encode('utf8'), hashed_password)
# -> False

# second attempt:
password = u"seCr3t"
bcrypt.checkpw(password.encode('utf8'), hashed_password)
# -> True

如果你的s1字符串没有任何空格,你也可以这样做。

答案 8 :(得分:0)

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

}