如果用户输入字母E / e,我试图替换数组中的对象。例如,数组可以包含2个对象,数组中的第一个对象,用户输入3,用户现在输入5 e,他们新的数组对象[0]成为新的用户输入5.这是我的代码到目前为止:< / p>
import java.text.*;
import java.io.*;
public class Lab1a {
public static void main (String argv []) throws IOException {
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
NumberFormat nf = NumberFormat.getNumberInstance ();
nf.setMaximumFractionDigits (2);
nf.setMinimumFractionDigits (2);
String inputValue;
double [] doubleValues = new double [1];
char char1, char2, char3;
inputValue = stdin.readLine ();
String [] Values2 = inputValue.split ("\\s+");
for (int i = 0; i < Values2.length; ++i)
doubleValues [i] = Double.parseDouble (Values2[i]);
double old = doubleValues[0];
double newNum = doubleValues[1];
//Line 3: 3 characters separated by spaces
inputValue = stdin.readLine ();
String [] Values3 = inputValue.split ("\\s+");
for (int i = 0; i < Values3.length; ++i);
char1 = Values3[0].charAt(0);
char2 = Values3[1].charAt(0);
char3 = Values3[2].charAt(0);
if (inputValues == 'double' + "e") {
System.out.println(old);
}
}
答案 0 :(得分:0)
所以我很快就把它鞭打了,它回答了你发布的问题。 它将您的数组初始化为固定的SIZE = 10,并要求用户输入,直到输入“q”。 如果用户输入'数字e / E',则最后输入的值将被替换。
希望有所帮助! 干杯!
import java.util.Scanner;
public class Lab1a {
public static void main(String[] args) {
final int SIZE =10;//SIZE of array, you can change that if you want to
int availableIndex =0;//available element to change
double [] values = new double [SIZE];
Scanner kb = new Scanner (System.in);
while(availableIndex < values.length){
System.out.println("Please enter a value, if you wish to modify the last input value, then please enter your new value followed by an E/e");
System.out.println("enter q to exit");
String input = kb.nextLine().trim();
if (input.equalsIgnoreCase("q"))System.exit(0);
String [] arr = input.split("\\s+");
//for (String s : arr)System.out.println(s);
//validate input
if (arr.length == 1){//no change requested
values [availableIndex++] = Double.parseDouble(arr[0]);
}
else{
if (arr[1].equalsIgnoreCase("e")){
values[(availableIndex-1)] = Double.parseDouble(arr[0]);//availableIndex++;
}
else{//user inputed a char other than e/E
values[availableIndex++]= Double.parseDouble(arr[0]);
}
}
//print contents
for (int i=0; i < availableIndex; i++){
System.out.print(values[i]+"; ");
}
System.out.println();
}
}
}