我是一个菜鸟程序员,但我一直坚持这一点代码。你如何重新开始?我已经尝试了几种不同的方法,但它们都要么花费大量的代码,要么无法正常工作。我一直在尝试在我的所有编程任务中实现这段“简单”的代码,但它还没有成功。谢谢!
P.S。我已经完成了作业。我只是想让它更“完整”。
public class OddProduct {
public static void main(String[] args) {
//Inputs from user
System.out.println("Enter an odd number");
Scanner input_odd = new Scanner(System.in);
int odd = input_odd.nextInt();
int oddproduct = 1;
//Multiplies all odd integers
for (int counter = 1; counter <= odd; counter = counter + 2){
oddproduct = oddproduct * counter;
}//end of for- loop
System.out.printf("\nThe product of all the odd integers up to %d is %d\n",
odd, oddproduct);
/* MY NOTES FOR RECURSE
if (odd%2 == 1){ proceed normally}
else if (odd%2 != 1) { HOW TO LOOP BACK???}
else { println = "Application closed"}
*/
}//end of main method
}//end of OddProduct class
答案 0 :(得分:0)
根据您的备注,我认为这是您需要的
Scanner input_odd = new Scanner(System.in);
int odd = 0;
while (odd % 2 != 1) { // fails first time && if user enters even number
System.out.println("Enter an odd number");
odd = input_odd.nextInt();
}