Java 有人能帮忙分类吗?
我很难为我的程序创建排序方法。我创建了一个程序,询问用户是否希望从a-Z添加、删除、搜索或排序。 这是我的程序代码Java 有人能帮忙分类吗?,java,database,arrays,sorting,Java,Database,Arrays,Sorting,我很难为我的程序创建排序方法。我创建了一个程序,询问用户是否希望从a-Z添加、删除、搜索或排序。 这是我的程序代码 import java.util.Scanner; import java.io.*; /** * Write a description of class Final_Project here. * * @author (your name) * @version (a version number or a date) */ public class Final_
import java.util.Scanner;
import java.io.*;
/**
* Write a description of class Final_Project here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Final_Project
{
public static void main (String args[])throws IOException
{
Scanner c = new Scanner(System.in);
Scanner a = new Scanner(System.in);
Scanner f = new Scanner(System.in);
Scanner g = new Scanner(System.in);
String car[][] = new String [20][4];
System.out.println("JD Car Inventory");
System.out.println();
System.out.println();
while (true)
{
System.out.println();
System.out.println("What would you like to do?");
System.out.println("a= Add vehicle");
System.out.println("b= Delete vehicle");
System.out.println("c= Search Inventory");
System.out.println("d= Sort Inventory");
System.out.println("e= Exit Program");
System.out.println("f= View database");
System.out.println();
String get = g.nextLine();
System.out.println();
if (get.equalsIgnoreCase("a")){
a(car);
}
else if(get.equalsIgnoreCase("b")){
del(car);
}
else if (get.equalsIgnoreCase("c")){
search(car);
}
else if (get.equalsIgnoreCase("d")){
sort(car);
}
else if (get.equalsIgnoreCase("e"))
{
System.out.println ("Thank you for visiting JD CAR'S.");
break;
}
else if (get.equalsIgnoreCase("f"))
{
BufferedReader input = new BufferedReader (new FileReader("name.txt"));
for (int y=0;y<20;++y)
{
for (int x=0;x<4;++x)
{
String line = input.readLine();
car[y][x] = line;
if (line == null)
{
car[y][x] = "";
}
}
}
input.close();
PrintWriter output = new PrintWriter (new FileWriter("name.txt"));
for (int y=0; y<20; y++) //rows
{
for (int x=0; x<4; x++) //columns
{
System.out.print (String.format("%1$-" + 32 + "s", car[y][x]));
output.println(car[y][x]);
}
System.out.println ();
System.out.println("------------------------------------------------------------------------------------------------------------------------");
//output.println();
//output.println("-------------------------------------------------------------------------------------");
}
output.close();
}
else
{
System.out.println ("This is invalid. Enter again.");
}
}
}
public static void a (String car[][])throws IOException
{
Scanner sc = new Scanner(System.in);
for (int y=11;y<20;++y)
{
for (int x=0;x<4;++x)
{
if (car[y][x].equals(""))
{
while (true)
{
System.out.println ("What is the name of the vehicle?");
car[y][x] = sc.nextLine ();
x+=1;
System.out.println ("How much Horsepower does it have?");
car[y][x] = sc.nextLine ();
x+=1;
System.out.println ("What is the Engine Size (L)?");
car[y][x] = sc.nextLine ();
x+=1;
System.out.println ("What is the price ($)?");
car[y][x] = sc.nextLine ();
x+=1;
System.out.println ("Would you like to add more?");
String add = sc.nextLine ();
}
}
}
}
}
public static void del (String car[][])throws IOException
{
Scanner l = new Scanner(System.in);
Scanner cs = new Scanner(System.in);
System.out.println("Do you want to remove any vehicle?");
String del = l.nextLine();
if (del.equalsIgnoreCase("yes")){
System.out.println("Enter the field number you would like to remove (1,2,3,4)");
double d= cs.nextDouble();
System.out.println();
if (d == 1){
car[1][0] = "";
car[1][1] = "";
car[1][2] = "";
car[1][3] = "";
}
else if (d == 2){
car[2][0] = "";
car[2][1] = "";
car[2][2] = "";
car[2][3] = "";
}
else if (d == 3){
car[3][0] = "";
car[3][1] = "";
car[3][2] = "";
car[3][3] = "";
}
else if (d == 4){
car[4][0] = "";
car[4][1] = "";
car[4][2] = "";
car[4][3] = "";
}
else if (d == 5){
car[5][0] = "";
car[5][1] = "";
car[5][2] = "";
car[5][3] = "";
car[5][4] = "";
car[5][5] = "";
}
else if (d == 6){
car[6][0] = "";
car[6][1] = "";
car[6][2] = "";
car[6][3] = "";
}
else if (d == 7){
car[7][0] = "";
car[7][1] = "";
car[7][2] = "";
car[7][3] = "";
}
else if (d == 8){
car[8][0] = "";
car[8][1] = "";
car[8][2] = "";
car[8][3] = "";
}
else if (d == 9){
car[9][0] = "";
car[9][1] = "";
car[9][2] = "";
car[9][3] = "";
}
else if (d == 10){
car[10][0] = "";
car[10][1] = "";
car[10][2] = "";
car[10][3] = "";
}
}
}
public static void search( String car[][])throws IOException
{
Scanner f = new Scanner(System.in);
Scanner a = new Scanner(System.in);
Scanner o = new Scanner(System.in);
while(true){
System.out.println ("0 = Car Name");
System.out.println ("1 = Horsepower");
System.out.println ("2 = Engine Size");
System.out.println ("3 = Price");
int cNumber = f.nextInt ();
System.out.println ("Enter what to search?");
String sA = a.nextLine ();
boolean flag=true;
for (int j=11;j<20;++j)
{
for (int k=0;k<4;++k)
{
if(sA.equals (car[j][cNumber]))
{
while (true)
{
flag=true;
int t=0;
int q=0;
System.out.print (String.format("%1$-" + 32 + "s", car[t][q]));
q+=1;
System.out.print (String.format("%1$-" + 32 + "s", car[t][q]));
q+=1;
System.out.print (String.format("%1$-" + 32 + "s", car[t][q]));
q+=1;
System.out.print (String.format("%1$-" + 32 + "s", car[t][q]));
q+=1;
System.out.println();
break;
}
}
}
if (flag == false )
{
System.out.println();
System.out.println ("No search resulst for" + sA + ". Trying to see if you spelt the word right.");
}
System.out.println();
System.out.println ("Would you like to search again?");
sA = o.nextLine ();
if (sA.equalsIgnoreCase ("no"))
{
break;
}
}
}
}
public static void sort (String car[][])throws IOException
{
}
}
}
import java.util.Scanner;
导入java.io.*;
/**
*在这里写一篇期末专题的描述。
*
*@author(你的名字)
*@version(版本号或日期)
*/
公开课期末考试项目
{
公共静态void main(字符串args[])引发IOException
{
扫描仪c=新扫描仪(System.in);
扫描仪a=新的扫描仪(System.in);
扫描器f=新扫描器(System.in);
扫描仪g=新的扫描仪(System.in);
字符串car[][]=新字符串[20][4];
系统输出打印项次(“JD汽车库存”);
System.out.println();
System.out.println();
while(true)
{
System.out.println();
System.out.println(“您想做什么?”);
System.out.println(“a=添加车辆”);
System.out.println(“b=删除车辆”);
System.out.println(“c=搜索库存”);
System.out.println(“d=分类库存”);
System.out.println(“e=退出程序”);
System.out.println(“f=视图数据库”);
System.out.println();
字符串get=g.nextLine();
System.out.println();
if(get.equalsIgnoreCase(“a”)){
a(汽车);
}
else if(get.equalsIgnoreCase(“b”)){
del(car);
}
else if(get.equalsIgnoreCase(“c”)){
搜查(汽车);
}
else if(get.equalsIgnoreCase(“d”)){
分拣(汽车);
}
else if(get.equalsIgnoreCase(“e”))
{
System.out.println(“感谢您访问京东汽车公司”);
打破
}
else if(get.equalsIgnoreCase(“f”))
{
BufferedReader输入=新的BufferedReader(新文件读取器(“name.txt”);
对于(int y=0;y我认为您不应该编写自己的排序方法,而是应该在array.sort
方法中利用Java的bulid
例如
由于这看起来像是一项研究,因此只有以下建议:
将问题分为已完成(最初为空)和待完成(最初为全部)。然后从待完成项中重复选择一个候选项并增加待完成项
有点像:
int sortedCars = 0;
while (sortedCars < car.length) {
// Pick the smallest from the unsorted cars:
int smallest = sortedCars;
for (int i = smallest + 1; i < car.length; i++) {
if (car[i] less than car[smallest]) {
smallest = i;
}
}
// Exchange car[sortedCars] with car[smallest]:
...
++sortedCars;
}
int-sortedCars=0;
while(分拣车<车厢长度){
//从未分类的车辆中选择最小的:
int最小值=分类卡;
对于(int i=最小值+1;i
您是否有排序代码,您是在问我们如何修复它,还是在问如何添加排序功能?另请注意,您可以在del
方法中折叠一大块代码-使用car[d][0]=”;
然后您不必比较每个值1…10(只需进行范围检查)。我要一个排序代码。Righty-ho。到目前为止你做了什么?添加此信息将为人们提供最相关的帮助线索。“没有搜索“+sA
的结果”应该是“没有搜索“+sA
+1的结果”以取消无法解释的否决票。请务必给出否决票的原因,伙计们。
int sortedCars = 0;
while (sortedCars < car.length) {
// Pick the smallest from the unsorted cars:
int smallest = sortedCars;
for (int i = smallest + 1; i < car.length; i++) {
if (car[i] less than car[smallest]) {
smallest = i;
}
}
// Exchange car[sortedCars] with car[smallest]:
...
++sortedCars;
}