如何将正值和负值(可能是分开的?)存储到数组中,并打印出来?
How do I store positive and negative value (may be separately?) into an array, and print them out?
我的程序会要求用户输入 10 个数字。正数被视为存款,负数被视为提款。完成后,程序应打印出用户输入的金额。
我的问题是我无法设置将它们存储到数组中的逻辑。我不知道如何将正值和负值分离到一个数组中。
这是我目前所做的:
package depandwith;
import java.util.*;
public class DepAndWith {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("TO DEPOSIT USE POSITIVE AND, TO WITHDRAW USE NEGATIVE VALUE.\n\n");
for (int i = 0; i < 10; i++) {
System.out.println("Enter amount: ");
int amount[] = new int[10];
amount[i] = input.nextInt();
if (amount[i] >= 0) {
//store it as deposited
} else {//if the amount is negative
//store it as withdrawn
}
}
//Printing the amounts:
for (int i = 0; i < 10; i++) {
System.out.println("Print out Deposited amount");
System.out.println("Print out Withdrawn amount");
}
}
}
首先,您要在每次迭代时创建一个新数组。移动行
int amount[] = new int[10];
到循环外。
其次,您不需要以不同方式处理它,您可以将任何 int
数字存储在 int
数组中。
如果您坚持要分隔 pos/neg 个数字,请创建两个数组,然后分隔..我看不出有任何理由在您的情况下这样做。
您要做的是正常存储它们。打印时注意它。
package depandwith;
import java.util.*;
public class DepAndWith {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("TO DEPOSIT USE POSITIVE AND, TO WITHDRAW USE NEGATIVE VALUE.\n\n");
int amount[] = new int[10];
for (int i = 0; i < 10; i++) {
System.out.println("Enter amount: ");
amount[i] = input.nextInt();
}
//Printing the deposit amounts:
for (int i = 0; i < 10; i++) {
System.out.print("Print out Deposited amount");
if(amount[i]>0){
System.out.print(amount[i]+", ")
}
}
System.out.println();
//Printing the withdraw amounts:
for (int i = 0; i < 10; i++) {
System.out.println("Print out Withdraw amount");
if(amount[i]<0){
System.out.print(amount[i]+", ")
}
}
}
}
我的程序会要求用户输入 10 个数字。正数被视为存款,负数被视为提款。完成后,程序应打印出用户输入的金额。
我的问题是我无法设置将它们存储到数组中的逻辑。我不知道如何将正值和负值分离到一个数组中。
这是我目前所做的:
package depandwith;
import java.util.*;
public class DepAndWith {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("TO DEPOSIT USE POSITIVE AND, TO WITHDRAW USE NEGATIVE VALUE.\n\n");
for (int i = 0; i < 10; i++) {
System.out.println("Enter amount: ");
int amount[] = new int[10];
amount[i] = input.nextInt();
if (amount[i] >= 0) {
//store it as deposited
} else {//if the amount is negative
//store it as withdrawn
}
}
//Printing the amounts:
for (int i = 0; i < 10; i++) {
System.out.println("Print out Deposited amount");
System.out.println("Print out Withdrawn amount");
}
}
}
首先,您要在每次迭代时创建一个新数组。移动行
int amount[] = new int[10];
到循环外。
其次,您不需要以不同方式处理它,您可以将任何 int
数字存储在 int
数组中。
如果您坚持要分隔 pos/neg 个数字,请创建两个数组,然后分隔..我看不出有任何理由在您的情况下这样做。
您要做的是正常存储它们。打印时注意它。
package depandwith;
import java.util.*;
public class DepAndWith {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("TO DEPOSIT USE POSITIVE AND, TO WITHDRAW USE NEGATIVE VALUE.\n\n");
int amount[] = new int[10];
for (int i = 0; i < 10; i++) {
System.out.println("Enter amount: ");
amount[i] = input.nextInt();
}
//Printing the deposit amounts:
for (int i = 0; i < 10; i++) {
System.out.print("Print out Deposited amount");
if(amount[i]>0){
System.out.print(amount[i]+", ")
}
}
System.out.println();
//Printing the withdraw amounts:
for (int i = 0; i < 10; i++) {
System.out.println("Print out Withdraw amount");
if(amount[i]<0){
System.out.print(amount[i]+", ")
}
}
}
}