在java上按一定量递减
我试图创建一个程序,用户输入的数字会减少一定数量。这是我想要的:在java上按一定量递减,java,loops,subtraction,decrement,Java,Loops,Subtraction,Decrement,我试图创建一个程序,用户输入的数字会减少一定数量。这是我想要的: Update by (Increment/Decrement):decrement Enter starting number:15 Enter update number:3 Enter end number:3 loop#1 value=15 loop#2 value=12 public class loop { public enum Operation {INCREMENT, DECREMENT, INVA
Update by (Increment/Decrement):decrement
Enter starting number:15
Enter update number:3
Enter end number:3
loop#1 value=15
loop#2 value=12
public class loop {
public enum Operation {INCREMENT, DECREMENT, INVALID}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Update by (Increment/Decrement):");
String operationString = scan.nextLine();
System.out.print("Enter starting number:");
String numberString = scan.nextLine();
System.out.print("Enter update number:");
String upnumberString = scan.nextLine();
System.out.print("Enter end number:");
String endnumberString = scan.nextLine();
// Determine and parse stuff
int startNumber = Integer.parseInt(numberString);
int updateNumber = Integer.parseInt(upnumberString);
int endNumber = Integer.parseInt(endnumberString);
// Parse operation, but assume invalid operation
Operation operation = Operation.INVALID;
if (operationString.equalsIgnoreCase("increment")) {
operation = Operation.INCREMENT;
} else if (operationString.equalsIgnoreCase("decrement")) {
operation = Operation.DECREMENT;
}
// now do the "meat" of the assignment
int loopNumber = 0; // we'll keep the loop number as a separate counter
switch (operation) {
case INCREMENT:
for (int i = startNumber; i < endNumber; i = i + updateNumber) {
loopNumber++;
performAssignmentPrinting(loopNumber, i);
}
break;
case DECREMENT:
for (int i = startNumber; i > endNumber; i = i - updateNumber) {
loopNumber++;
performAssignmentPrinting(loopNumber, i)
}
break;
case INVALID:
default:
throw new IllegalStateException("Please enter supported operation! (increment/decrement)");
}
}
private static void performAssignmentPrinting(int loopNumber, int value) {
System.out.println("loop#" + loopNumber + "\tvalue=" + value);
}
}
结束编号是循环将停止的位置,更新编号是起始编号应减少多少。到目前为止,这是我的代码,我被困在如何保持循环,直到结束数字
package jaba;
import java.util.Scanner;
public class loop {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Scanner input = new Scanner(System.in);
System.out.print("Update by (Increment/Decrement):");
String Decrement = scan.nextLine();
System.out.print("Enter starting number:");
String number = scan.nextLine();
System.out.print("Enter update number:");
String upnumber = scan.nextLine();
System.out.print("Enter end number:");
String endnumber = scan.nextLine();
int i,j;
i = 15;
j = 1;
do {
System.out.println("loop#" +j+ "\tvalue="+i);
j++;
}while(i<15);
i = i-3;
System.out.println("loop#" +j+ "\tvalue="+i);
};
}
包jaba;
导入java.util.Scanner;
公共类循环{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
扫描仪输入=新扫描仪(System.in);
系统输出打印(“更新方式(增量/减量):”;
字符串减量=scan.nextLine();
系统输出打印(“输入起始编号:”);
字符串编号=scan.nextLine();
系统输出打印(“输入更新编号:”);
字符串upnumber=scan.nextLine();
系统输出打印(“输入结束编号:”);
String endnumber=scan.nextLine();
int i,j;
i=15;
j=1;
做{
System.out.println(“循环#“+j+”\tvalue=“+i”);
j++;
}而(iFor循环是程序的解决方案。For(a;b;c){…}
谷歌搜索for循环是如何工作的。试着了解a、b、c三部分是如何工作的
伪:
// if decrease mode
// for (i = upperbound ; i >= lowerbound ; i-= decrement)
// print i
// if increase mode
// for (i = lowerbound ; i <= upperbound ; i+= increment)
// print i
//如果减少模式
//for(i=上限;i>=下限;i-=减量)
//打印i
//如果增加模式
//for(i=lowerbound;ifor循环是程序的解决方案。for(a;b;c){…}
谷歌搜索for循环是如何工作的。试着了解a、b、c三部分是如何工作的
伪:
// if decrease mode
// for (i = upperbound ; i >= lowerbound ; i-= decrement)
// print i
// if increase mode
// for (i = lowerbound ; i <= upperbound ; i+= increment)
// print i
//如果减少模式
//for(i=上限;i>=下限;i-=减量)
//打印i
//如果增加模式
//对于(i=lowerbound;i这样的东西怎么样:
Update by (Increment/Decrement):decrement
Enter starting number:15
Enter update number:3
Enter end number:3
loop#1 value=15
loop#2 value=12
public class loop {
public enum Operation {INCREMENT, DECREMENT, INVALID}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Update by (Increment/Decrement):");
String operationString = scan.nextLine();
System.out.print("Enter starting number:");
String numberString = scan.nextLine();
System.out.print("Enter update number:");
String upnumberString = scan.nextLine();
System.out.print("Enter end number:");
String endnumberString = scan.nextLine();
// Determine and parse stuff
int startNumber = Integer.parseInt(numberString);
int updateNumber = Integer.parseInt(upnumberString);
int endNumber = Integer.parseInt(endnumberString);
// Parse operation, but assume invalid operation
Operation operation = Operation.INVALID;
if (operationString.equalsIgnoreCase("increment")) {
operation = Operation.INCREMENT;
} else if (operationString.equalsIgnoreCase("decrement")) {
operation = Operation.DECREMENT;
}
// now do the "meat" of the assignment
int loopNumber = 0; // we'll keep the loop number as a separate counter
switch (operation) {
case INCREMENT:
for (int i = startNumber; i < endNumber; i = i + updateNumber) {
loopNumber++;
performAssignmentPrinting(loopNumber, i);
}
break;
case DECREMENT:
for (int i = startNumber; i > endNumber; i = i - updateNumber) {
loopNumber++;
performAssignmentPrinting(loopNumber, i)
}
break;
case INVALID:
default:
throw new IllegalStateException("Please enter supported operation! (increment/decrement)");
}
}
private static void performAssignmentPrinting(int loopNumber, int value) {
System.out.println("loop#" + loopNumber + "\tvalue=" + value);
}
}
像这样的怎么样:
Update by (Increment/Decrement):decrement
Enter starting number:15
Enter update number:3
Enter end number:3
loop#1 value=15
loop#2 value=12
public class loop {
public enum Operation {INCREMENT, DECREMENT, INVALID}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Update by (Increment/Decrement):");
String operationString = scan.nextLine();
System.out.print("Enter starting number:");
String numberString = scan.nextLine();
System.out.print("Enter update number:");
String upnumberString = scan.nextLine();
System.out.print("Enter end number:");
String endnumberString = scan.nextLine();
// Determine and parse stuff
int startNumber = Integer.parseInt(numberString);
int updateNumber = Integer.parseInt(upnumberString);
int endNumber = Integer.parseInt(endnumberString);
// Parse operation, but assume invalid operation
Operation operation = Operation.INVALID;
if (operationString.equalsIgnoreCase("increment")) {
operation = Operation.INCREMENT;
} else if (operationString.equalsIgnoreCase("decrement")) {
operation = Operation.DECREMENT;
}
// now do the "meat" of the assignment
int loopNumber = 0; // we'll keep the loop number as a separate counter
switch (operation) {
case INCREMENT:
for (int i = startNumber; i < endNumber; i = i + updateNumber) {
loopNumber++;
performAssignmentPrinting(loopNumber, i);
}
break;
case DECREMENT:
for (int i = startNumber; i > endNumber; i = i - updateNumber) {
loopNumber++;
performAssignmentPrinting(loopNumber, i)
}
break;
case INVALID:
default:
throw new IllegalStateException("Please enter supported operation! (increment/decrement)");
}
}
private static void performAssignmentPrinting(int loopNumber, int value) {
System.out.println("loop#" + loopNumber + "\tvalue=" + value);
}
}
您有i=i-3;
out-of-loop。
将i的递减移动到循环中:
do {
System.out.println("loop#" + j + "\tvalue=" + i);
j++;
i = i - 3;
} while (i > endnumber);
您有i=i-3;
out-of-loop。
将i的递减移动到循环中:
do {
System.out.println("loop#" + j + "\tvalue=" + i);
j++;
i = i - 3;
} while (i > endnumber);
提示:您的do/while
循环有一个取决于i
的终止条件,但循环体不会更改i
…因此它将只执行一次或永远执行。提示:您的do/while
循环有一个取决于i
的终止条件,但循环体不会更改E>我<或代码>…所以它将执行一次或全部。当我理解这是一个学校作业时,考虑下面的输入:<代码> StastNox= 1000 ,<代码> EndoNo.= -1000 < <代码>,<代码> UpDeNoNoST=100 < /Cord>,并查看当您输入增量和减量时会发生什么。注意循环数WH的差异。如果我没弄错的话,它应该在21474846左右。CC@coderx这个解决方案可能对我上面提到的输入也不起作用,因为它假定在递增的情况下,开始数将小于结束数,反之亦然,递减。@coderx添加了do/while版本,也应该对c起作用请看我在第一条评论中提到的示例。您好!非常感谢您提供的附加信息和源代码:)源代码确实帮助我理解了作业的逻辑和概念!再次感谢您!@ CordEX为加分,添加一个输入验证检查来警告用户,如果他输入“代码>0”/代码>更新编号,并提示输入一次。我理解这是一个学校作业,考虑以下输入:<代码>>startNumber=1000
,endNumber=-1000
,updateNumber=100
,然后查看当您输入递增和递减时会发生什么。请注意递增时循环数的差异。如果我没有弄错的话,它应该在21474846左右。CC@CoderX此解决方案也可能不适用于我所述的输入above,因为它假定在递增的情况下,开始数将小于结束数,反之亦然,递减。@coderx添加了do/while版本,该版本也适用于我在第一条评论中所述的反例。您好!非常感谢您提供的附加信息和源代码:)源代码真的帮助我理解了分配的逻辑和概念!再次非常感谢!@coderx获得额外分数,添加输入验证检查,如果用户输入0
以获取更新编号,则警告用户,并再次提示输入。