2016-02-04 39 views
0

我想要替换数组中的对象,如果用户输入字母E/e。例如,数组可以包含2个对象,数组中的第一个对象用户输入一个3,用户现在输入一个5e,他们新的数组对象[0]变成新的用户输入5.这是我的代码到目前为止:尝试比较一个数组项目与useres输入

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

请包括一个具体的问题。 – Spencer4134

+0

你最后一个for循环只是循环而不做任何事情。循环后摆脱';'。 –

+0

你的问题不是很清楚。用样本输入输出解释 – thedarkpassenger

回答

0

所以我很快就把这个提起来了,它回答了你发布的问题。 它将您的数组初始化为固定SIZE = 10,并要求用户输入,直到输入'q'。 如果用户输入'digit 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(); 
    } 
} 



} 
+0

FYI:没有异常处理,代码假定用户将输入:{value}或{value} SPACE {e/E}或{q} – Fadi