Printing 为什么赢了';它不是打印出来的吗?初学者查询
这是一个打印任意给定整数之间所有偶数的程序Printing 为什么赢了';它不是打印出来的吗?初学者查询,printing,integer,zero,out,Printing,Integer,Zero,Out,这是一个打印任意给定整数之间所有偶数的程序 import java.util.*; public class Question1 { private int i; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Give me a number!"); int i
import java.util.*;
public class Question1
{
private int i;
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Give me a number!");
int i = scanner.nextInt();
if ((i % 2) != 0)
{
i = i - 1;
do
{
System.out.println(i);
i = i - 2;
} while (i != -2);
}
}
}
所以,如果我给数字11,它会打印出10,8,6,4,2。既然我的while语句包含i,为什么它不同时打印0!=-2和0算作偶数?因为在
scanner.nextInt()之后代码>必须放置scanner.nextLine()
else,扫描仪从nextInt()获取的最后一个元素代码>将被忽略
即便如此,您的算法还是非常令人眩晕。为什么不试试:
Scanner in = new Scanner( System.in );
int number = in.nextInt(); in.nextLine();
for( int i = 0; i <= number; i += 2 ) {
System.out.println( i );
}
Scanner-in=新的扫描仪(System.in);
int number=in.nextInt();in.nextLine();
对于(int i=0;i,因为在scanner.nextLine();
之后必须放置scanner.nextLine();
否则,将忽略扫描仪从nextInt();
获取的最后一个元素
即便如此,你的算法还是令人头晕目眩。为什么不试试:
Scanner in = new Scanner( System.in );
int number = in.nextInt(); in.nextLine();
for( int i = 0; i <= number; i += 2 ) {
System.out.println( i );
}
Scanner-in=新的扫描仪(System.in);
int number=in.nextInt();in.nextLine();
对于(int i=0;i