私の課題は、コンピュータ ソフトウェアの在庫を確認するための簡単な Java プログラムを作成することです。ソフトウェアの名前と在庫数をユーザーが入力するプログラムを設計します。
次に、プログラムは、レコードが在庫数順に並び、すべてのレコードが番号順に表示されるようにアイテムをソートする必要があります。
私は2つの方法を使用する必要があります!ユーザー入力を取得して配列に格納する方法と、最小から最大の順に並べ替えて表示する方法です!
これが私のコードです。次に何をすべきかわかりません。選択 sortを使用してソートする方法がよくわかりませんが、それを使用する必要があります。あそこにあるのはレッスンで使ったもの!inputInfoメソッドでまだ使用されていない変数を初期化する方法がわからなかったため、 displayInfoメソッドが間違っていると確信しています(配列などが必要です)。そして、メインでdisplayInfoメソッドを呼び出すと機能しません。
それが理にかなっていることを願っています。説明の仕方がよくわからなかった…
助けてください!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
public class ComputersRUs {
public static void inputInfo() throws IOException
{
BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
System.out.print("How many softwares would you like to input? ");
String software = userInput.readLine();
int softwareNum = Integer.parseInt(software);
int[] softArray = new int[softwareNum];
String [] name = new String [softwareNum];
int [] quantity = new int[softwareNum];
//loop through number of softwares
for (int i = 0; i < softwareNum; i++)
{
System.out.println("Input name of software: ");
String softwareName = userInput.readLine();
name[i] = softwareName;
System.out.println("Input quantity of software: ");
String quantityString = userInput.readLine();
int softwareQuantity = Integer.parseInt(quantityString);
quantity[i] = softwareQuantity;
System.out.println("There are " + quantity[i] + " of the " + name[i] + " software.");
}
}
//method to sort and display info
public static void displayInfo(int[] arr)
{
//sort by quantity
for(int i=0; i<arr.length; i++)
{
for(int j=i+1; j<arr.length; j++)
{
if(arr[i] > arr[j] )
{
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
//output
for(i=0; i<=arr.length; i++)
{
System.out.println(arr[i] + " ");
}
}
}
//main
public static void main(String[] args) throws IOException {
//input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//loop to stop when user requests
String quit = "n";
while("n".equals(quit))
{
//display menu
System.out.println("'COMPUTERS R US' Software Inventory - Main Menu");
System.out.println("1) Input information");
System.out.println("2) Display information in order");
System.out.println("3) Quit program");
System.out.print("Please choose an option by inputting the number of your choice: ");
String choiceString = br.readLine();
int choice = Integer.parseInt(choiceString);
if(choice == 1)
{
inputInfo();
}else if(choice == 2)
{
displayInfo();
}else if(choice == 3)
{
System.out.println("Are you sure you want to quit? (y/n) ");
quit = br.readLine();
}else
{
System.out.println("Not a valid option.");
}
}
}
}
ありがとうございました!