gpt4 book ai didi

java - 如何在我的代码中集成循环

转载 作者:行者123 更新时间:2023-12-01 09:13:51 25 4
gpt4 key购买 nike

我是 Java 新手。我的程序首先从用户那里获取有关其汽车的输入,然后显示结果。我需要将我的“Rövarspråk”集成到代码中,但我不太确定如何集成。如果用户拥有“Saab”或“Volvo”,则“rövarspråk”循环应更改用户的“字符串名称”。如果有不清楚的地方,请告诉我,我会尽力解释得更好。

提前致谢。

public static void main(String[] args) {
String lookSaab;

String consonantsx;
String input;
String slang;
String add;
// String
int length;
// int
Scanner skriv;
// Scanner

String reg;
String year;
String brand;
String name;
String car;
String when;
String small;
String medium;
String big;
// String
int mod;
int randomNumber;
int quota;
int denominator;
// int







reg = JOptionPane.showInputDialog("Ange registreringsnummer"); // Input plate number of your car
year = JOptionPane.showInputDialog("Ange årsmodell"); // Input model year of the car
mod = Integer.parseInt(year);
brand = JOptionPane.showInputDialog("Ange bilmärke"); //Input car brand
name = JOptionPane.showInputDialog("Ange ägare "
+ "(för - och efternamn)"); //Input owner of the car first name + last name
car = brand + reg;
Date date = new Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("EEE MMM dd");
when = sdf.format(date);
denominator = 1500;
randomNumber = 1500 + (int)(Math.random() * ((40000 - 1500) + 1));
quota = randomNumber / denominator;

small = "Liten service";
medium = "Medium service";
big = "Stor service";
if (randomNumber <= 8000){
JOptionPane.showMessageDialog(null, small, "Typ av service", 1);
} else if ( randomNumber <= 20000){
JOptionPane.showMessageDialog(null, medium, "Typ av service", 1);
} else {
JOptionPane.showMessageDialog(null, big, "Typ av service", 1);
}

String resultat = "Bil: " + car + "\n"
+ "Årsmodell: " + mod + "\n"
+ "Ägare: " + name + "\n"
+ "Mästarställning: " + randomNumber + "\n"
+ "Inlämnad: " + when + "\n"
+ "Klar om: " + quota + " dagar";
JOptionPane.showMessageDialog(null, resultat, "Resulat", 1);

lookSaab = "Saab";

if (brand.equals(lookSaab)){

}

/* Rövarspråket */



consonantsx = "bBcCdDeEfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ"; //Saves all consonants to string
char consonants[] = consonantsx.toCharArray(); //String to charr

System.out.println("Mata in en mening");
skriv = new Scanner(System.in);
input = skriv.nextLine(); //Saves the input
length = input.length(); //Length inc. space
char array[] = input.toCharArray(); // Input to a char array

slang = "";
System.out.println("På rövarspråk:");
for(int i = 0; i<length; i++) {
for(int x = 0; x<20; x++){
if(array[i] == consonants[x])
{
add = array[i]+"o"+array[i];
slang = slang + add;
break;
}
else{
}
}
}
System.out.println(slang);
}
}

最佳答案

好的,正如前面提到的,一个好的开始是将您的 RoverSpraket 翻译器放入其自己的方法中:

public String rovarSpraket(String normalString) {

final String consonantsx = "bBcCdDeEfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ";
char consonants[] = consonantsx.toCharArray(); // String to charr

int length = normalString.length(); // Length inc. space
char array[] = normalString.toCharArray(); // Input to a char array

String slang = "";
System.out.println("På rövarspråk:");
for (int i = 0; i < length; i++) {
for (int x = 0; x < 20; x++) {
if (array[i] == consonants[x]) {
String add = array[i] + "o" + array[i];
slang = slang + add;
break;
} else {
}
}
}
return slang;
}

此方法采用“正常”字符串作为输入并返回它的 Rövarspråk 版本。

鉴于它现在可以在任何您想要的地方使用,例如:

 /i named my class "Goran" ;)
Goran goran = new Goran(); //instatiate a class object

String hello = "hello world";

System.out.println(goran.rovarSpraket(hello)); //use class object method "roverSpraket"

这将在控制台上打印如下:

På rövarspråk:霍赫埃洛洛洛多德

剩下要做的唯一一件事就是在剩余的代码中使用它。我想你想要的是:

if (brand.equals("Saab") || brand.equals("Volvo")){
name = rovarSpraket(name); //translate if brand is Saab or Volvo
}

以及调用该方法的工作示例(一种方法)

    public class Goran {

public static void main(String[] args) {
String brand;
String name;

//i named my class "Goran" ;)
Goran goran = new Goran(); //instatiate a class object

String hello = "hello world";

System.out.println(goran.rovarSpraket(hello)); //use class object method "roverSpraket"

brand = "Saab";
name = "henry";

if (brand.equals("Saab") || brand.equals("Volvo")){
name = goran.rovarSpraket(name); //translate if brand is Saab or Volvo
}

System.out.println("new name is " + name);

}

public String rovarSpraket(String normalString) {

final String consonantsx = "bBcCdDeEfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ";
char consonants[] = consonantsx.toCharArray(); // String to charr

int length = normalString.length(); // Length inc. space
char array[] = normalString.toCharArray(); // Input to a char array

String slang = "";
System.out.println("På rövarspråk:");
for (int i = 0; i < length; i++) {
for (int x = 0; x < 20; x++) {
if (array[i] == consonants[x]) {
String add = array[i] + "o" + array[i];
slang = slang + add;
break;
} else {
}
}
}
return slang;
}
}

希望这对您有帮助^^

关于java - 如何在我的代码中集成循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40727319/

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