How to calculate! Construct an algorithm that allows you to enter a 4-digit integer and calculate the sum of the first and last digits

huangapple go评论66阅读模式
英文:

How to calculate! Construct an algorithm that allows you to enter a 4-digit integer and calculate the sum of the first and last digits

问题

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Ingresa el número: ");
    Integer numero = sc.nextInt();
    char[] nums = numero.toString().toCharArray();
}
英文:
public static void main(String[] args) {
    
    Scanner sc = new Scanner (System.in);
    System.out.println("Ingresa el número: ");
    Integer numero = sc.nextInt();
    char[] nums = numero.toString().toCharArray();
}

答案1

得分: 1

我认为这就是你所寻找的内容。这个算法会首先检查数字中的位数,如果位数为4,则允许继续处理,否则会提示只允许4位整数。

    Scanner sc = new Scanner(System.in);
    System.out.println("请输入数字:");
    Integer number = sc.nextInt();
    if (Math.floor(Math.log10(number)) + 1 != 4) {
        System.out.println("只允许输入四位整数");
    } else {
        int last = number % 10;
        int first = number / 1000;
        System.out.println(last + first);
    }
英文:

I think this is what you are looking for. This algo will first check for the number of digits in the number, if they are 4 then allow to process further else it will say integer of 4 digits is allowed.

Scanner sc = new Scanner (System.in);
	    System.out.println("Ingresa el número: ");
	    Integer numero = sc.nextInt();
	    if(Math.floor(Math.log10(numero)) + 1 != 4 ) {
	    	System.out.println("Integer should be of four digits");
	    } else {
	    	int last = numero%10;
	    	int first = numero/1000;
	    	System.out.println(last+first);
	    }

答案2

得分: 1

这是你想要的:

int answer = n%10 + n/1000;

它将数字 n 的第一个和最后一个数字相加。

英文:

I think this is what you want :

int answer = n%10 + n/1000;

It adds the first and last digit of the number n.

答案3

得分: 0

你可以这样做:

Scanner sc = new Scanner(System.in);
System.out.println("请输入数字:");
String number = sc.nextLine();

Integer result = Integer.parseInt(String.valueOf(number.charAt(0))) +
    Integer.parseInt(String.valueOf(number.charAt(number.length() - 1)));
英文:

You can do something like this

Scanner sc = new Scanner (System.in);
System.out.println("Ingresa el número: ");
String numero = sc.nextLine();

Integer result = Integer.parseInt(numero.charAt(0)+"") +
    Integer.parseInt(number.charAt(numero.length()-1)+"");

答案4

得分: 0

你的意思是像这样吗?

public class Adder {

    public static void main(String[] args) {
        int num = 2008;

        // 寻找最后一位数字
        int sum = num % 10;
        // 寻找第一位数字
        while (num >= 10) {
            num /= 10;
        }
        sum += num;

        System.out.println(sum);
    }

}
英文:

You mean like this?

public class Adder {

	public static void main(String[] args) {
		int num=2008;
		
		//Find last number
		int sum = num%10;
		//Find first number
		while (num >= 10){
            num /= 10;
		}
		sum +=num;

		System.out.println(sum);
	}

}

huangapple
  • 本文由 发表于 2020年6月29日 14:13:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/62632247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定