Question 1(解决输入Scanner一系列的问题)

xiaoxiao2025-04-21  15

package question1; import java.util.Scanner; public class question1 { public static void main(String args[]) { Scanner n = new Scanner(System.in); System.out.println("用户请输入正整数"); String m = n.next(); int sum=0; if(m!=null && m.matches("^[0-9]+$")){ int x = Integer.parseInt(m); if((x==0) || (x==1)) { System.out.println("no odd"); System.out.println("no even"); } for(int i=0;i<x;i++) { if(i % 2!=0) { System.out.print(i+"\t"); } } System.out.println(); if(x==2) { System.out.println("no even"); } for(int i=0;i<x;i++) { if(i % 2==0) { sum+=sum+i; } } if(sum!=0) { System.out.println(sum); } }else{ System.out.println("格式不正确"); } } }

After receives a valid input,the program prints out:(1)all the positive odd numbers which are smaller than the input, and (2) the sum of all the even numbers which are smaller than the input.

转载请注明原文地址: https://www.6miu.com/read-5028806.html

最新回复(0)