简体   繁体   English

接受由分隔符分隔的两个整数并打印它们的总和

[英]Accept two integers separated by a delimiter and print their sum

import java.util.Scanner;

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt().split(":");
        int B = sc.nextInt();
        System.out.println(A + B);
    }
}

If I'm given an input like 1:2 then the output should be 3 .如果我得到像1:2这样的输入,那么 output 应该是3 Likewise 54:6 then 60 .同样54:6然后60

But I'm getting an error.但我遇到了一个错误。 What should I do to achieve that output?我应该怎么做才能实现 output?

You cannot call split on an integer, it is meant for splitting a String.您不能在 integer 上调用 split,它用于拆分字符串。 Try this:尝试这个:

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] numbers = sc.next().split(":");
        int A = Integer.parseInt(numbers[0]);
        int B = Integer.parseInt(numbers[1]);
        System.out.println(A + B);
    }
}

Of course some validation would be nice (check if the String contains a colon, if the parts are numeric, etc.), but this should point you in the right direction.当然,一些验证会很好(检查字符串是否包含冒号,部分是否为数字等),但这应该为您指明正确的方向。

At first, read a whole input line into a String variable.首先,将整个输入行读入一个字符串变量。 Then just split it into two values and cast them to integer.然后只需将其拆分为两个值并将它们转换为 integer。

Code example:代码示例:

Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
String[] splittedValues = inputString.split(":");
int 
    A = Integer.parseInt(splittedValues[0]),
    B = Integer.parseInt(splittedValues[1]); 

System.out.println(A + B);

you will need to take input as string and then split your input by: and convert the string to integer and add them.您需要将输入作为字符串,然后将输入拆分为: 并将字符串转换为 integer 并添加它们。

see example below见下面的例子


public class Hello {

    public static void main(String[] args) {
        String input;
        Scanner sc = new Scanner(System.in);
        input = sc.next();
        String[] parts = input.split(":");
       if(parts.length > 0) {
           int sum = Integer.parseInt(parts[0])+Integer.parseInt(parts[1]); 
           System.out.println(sum);
       } else{
             System.out.println("Enter number in format example 12:2");
       }
    }
}```
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
sc.next();
int B = sc.nextInt();
System.out.println(A + B);

import java.util.Scanner;导入 java.util.Scanner;

public class Hello {公共 class 你好{

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    sc.next().charAt(0);
    int B = sc.nextInt();
    System.out.println(A + B);
}

} }

I think Splitting of two integers is not possible in java(it is possible in python by using the input.split() function),for that reason it is better take input as string and split input by using colon(:) operator and convert those input string to an integer and add the both to print the result.我认为在 java 中无法拆分两个整数(在 python 中可以使用 input.split() 函数),因此最好将输入作为字符串并使用冒号(:) 运算符拆分输入并转换那些输入字符串到 integer 并添加两者以打印结果。 java code: java 代码:

   import java.util.Scanner;
   Public class TwoIntegers
   {
    public static void main(String args[])
    {
     Scanner s=new Scanner(System.in());
     String[] two_numbers = s.next().split(":");
     int fir_num = Int.parseInt(two_numbers[0]);
     int sec_num = Int.parseInt(two_numbers[1]);
     int sum=fir_num+sec_num;
     System.out.println("The sum of two numbers is:"+sum);
    }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM