gpt4 book ai didi

java - 如何从扫描仪中的数组打印数字

转载 作者:行者123 更新时间:2023-12-01 13:42:38 26 4
gpt4 key购买 nike

当扫描仪从数组中询问一个人时,试图打印出一个人选择的城市,但我需要帮助来实现它。

// Construct a Scanner.
Scanner scanner = new Scanner(System.in);

// Not a city value.
int city = -1;

// Forever...
while (true) {

// Display the prompt.
System.out.print("Please enter a destination city (1-12)" +
"1.SFO 2.ATL 3.DET 4.LA 5.CHI 6.NJ " +
"7.LV 8.MIA 9.BOS 10.DAL 11.DC 12.NY: ");

// check for an integer.
if (scanner.hasNextInt()) {

// get the value.
city = scanner.nextInt();

} else {

if (!scanner.hasNext()) {
// no more to read.
break;
}

// toss the value.
scanner.next();

// not an integer.
continue;
}

// check the range.
if (city >= 1 && city <= 12) {
break;
}

// bad value.
city = -1;
}

scanner.close();

我需要添加什么才能从数组中返回某个数字到目前为止,如果我的代码打印出这个

0 0 0 0
1 1 50 0
2 1 35 0
3 1 55 2
4 1 110 1
5 1 65 3
6 1 80 2
7 1 70 5
8 1 105 5
9 1 105 6
10 1 90 7
11 1 155 8
12 1 100 10

并且只需要第三列中的数字即可打印出来

enter code import java.util.*;

public class path {
public static int vertex = 0;

public static void main(String[] args){

String[] cities = new String[] { "SFO", "ATL",
"DET", "LA", "CHI", "NJ", "LV", "MIA", "BOS",
"DAL", "DC", "NY" };

Scanner scanner = new Scanner(System.in); // Construct a
// Scanner.
int city = -1; // Not a city value.
while (true) { // Forever...
// Display the prompt.
System.out
.println("Please enter a destination city (1-12): ");
for (int i = 0; i < cities.length; i++) { // print the city options.
System.out.println((1 + i) + ". " + cities[i]); // 1 to cities.length
}
if (scanner.hasNextInt()) { // check for an integer.
city = scanner.nextInt(); // get the value.
} else {
scanner.next(); // toss the value.
continue; // not an integer.
}
if (city >= 1 && city <= 12) { // check the range.
break;
}
city = -1; // bad value.
}
scanner.close(); // Close the Scanner.
if (city != -1) {
System.out.println("You selected " + cities[city - 1]);
System.out.println("It will cost you");
} else {
System.out.println("No valid city");
}


int[][] destination = new int[13][4];
destination[0][0]= 0;
destination[1][0]= 1;
destination[2][0]= 2;
destination[3][0]= 3;
destination[4][0]= 4;
destination[5][0]= 5;
destination[6][0]= 6;
destination[7][0]= 7;
destination[8][0]= 8;
destination[9][0]= 9;
destination[10][0]= 10;
destination[11][0]= 11;
destination[12][0]= 12;

for(int i = 0; i < 13; i++){
destination[i][1] = 0;
}
destination[0][3] = 0;
for(int j = 1; j < 13; j++){
destination[j][2] = Integer.MAX_VALUE;
}
for(int k = 0; k < 13; k++){
destination[k][3] = 0;
}

int[][] edge = new int[13][13];
edge[0][1] = 50;
edge[0][2] = 35;
edge[1][3] = 70;
edge[1][4] = 60;
edge[1][5] = 35;
edge[2][3] = 20;
edge[2][5] = 60;
edge[2][6] = 45;
edge[3][5] = 10;
edge[4][7] = 30;
edge[5][7] = 5;
edge[5][8] = 40;
edge[6][8] = 30;
edge[6][9] = 25;
edge[7][10] = 20;
edge[8][11] = 50;
edge[9][12] = 20;
edge[10][12] = 10;
edge[11][12] = 15;

update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);
update(destination,edge);
findV(destination);

for(int i = 0; i < destination.length; i++)
{
for(int j = 0; j < destination[i].length; j++)
{
System.out.print(destination[i][j]);
if(j < destination[i].length - 1) System.out.print(" ");
}
System.out.println();

}
}

public static void findV(int[][] destination){
int min = Integer.MAX_VALUE;
for(int a = 1; a < 13; a++){
if(destination[a][1] == 0){
if(destination[a][2] < min){
min = destination[a][2];
vertex = a;
}
}
}
destination[vertex][1] = 1;
//update();
}

public static void update(int[][] destination, int[][] edge)
{
for(int x = 0; x < 13; x++){
if(edge[vertex][x] != 0 && edge[vertex][x] + destination[vertex][2] <
destination[x][2] && destination[x][1] == 0){
destination[x][2] = edge[vertex][x] + destination[vertex][2];
destination[x][3] = vertex;
}

}

}
}

最佳答案

我必须说你的代码看起来很熟悉,试试这样的东西

public static void main(String[] args) {
// First, construct the array of cities,
String[] cities = new String[] { "SFO", "ATL",
"DET", "LA", "CHI", "NJ", "LV", "MIA", "BOS",
"DAL", "DC", "NY" };

Scanner scanner = new Scanner(System.in); // Construct a
// Scanner.
int city = -1; // Not a city value.
while (true) { // Forever...
// Display the prompt.
System.out
.println("Please enter a destination city (1-12): ");
for (int i = 0; i < cities.length; i++) { // print the city options.
System.out.println((1 + i) + ". " + cities[i]); // 1 to cities.length
}
if (scanner.hasNextInt()) { // check for an integer.
city = scanner.nextInt(); // get the value.
} else {
scanner.next(); // toss the value.
continue; // not an integer.
}
if (city >= 1 && city <= 12) { // check the range.
break;
}
city = -1; // bad value.
}
scanner.close(); // Close the Scanner.
if (city != -1) {
System.out.println("You selected " + cities[city - 1]);
} else {
System.out.println("No valid city");
}
}

关于java - 如何从扫描仪中的数组打印数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20591608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com