在JAVA程序中更改一维数组中所选元素的值

时间:2016-04-04 02:18:54

标签: java arrays for-loop methods

这是我的程序,它将成绩(元素)存储到成绩簿(数组)中 enter image description here

我基本上试图允许用户能够在Gradebook(数组)中多次改变他们想要的(元素)等级(基本上是循环)。我已经给用户提供了输入索引的选项(应该从1开始而不是0,以避免混淆用户)但是在这一步之后我被卡住了。我不确定如何搜索数组中的元素,然后要求用户替换它......

到目前为止,这是我的代码:

public async Task TaskRun(CancellationToken ct)
{
    ct.ThrowIfCancellationRequested();

    SpeechSynthesizer _speechSynthesizer = new SpeechSynthesizer();

    using (ct.Register(() => _speechSynthesizer.SpeakAsyncCancelAll())) {
        await _speechSynthesizer.SpeakAsync("This is a test prompt");
    }
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

结构应与此类似:

 System.out.println("___________________________");

 System.out.println("Make changes? Enter Y or N");
 String makeChanges = System.console().readLine();
 while (makeChanges.equals("Y")) {
  //Ask user if what grade they would like to change
  int index = NumberReader.readPositiveInt(input,
   "Enter the index of the grade to be changed: (1 to " + grades + ") : ", "Invalid index input");

  System.out.println("Enter grade (limit to two decimal places)" + index + ": ");
  //offset the index by one
  mogrades[index - 1] = NumberReader.readPositiveDouble(input, "Enter grade " + index + " :", "Invalid data entered");

  System.out.println("Make changes? Enter Y or N");
  makeChanges = System.console().readLine();
 }
}

您希望在他们仍想进行更改时继续循环。