Java 在NetBeans IDE中生成随机字?
我想问一下如何制作一个程序,生成三个长度为10个字符的单词,第一个字母是大写的,然后这三个单词应该合并,这样只有第一个字母是大写的。有一条注释说,要使第一个字母大写,你应该先用小写字母,然后用子字符串增加第一个字母。我假设它是用Java编写的(可能与C相同?) 看看Ascii表()Java 在NetBeans IDE中生成随机字?,java,netbeans,Java,Netbeans,我想问一下如何制作一个程序,生成三个长度为10个字符的单词,第一个字母是大写的,然后这三个单词应该合并,这样只有第一个字母是大写的。有一条注释说,要使第一个字母大写,你应该先用小写字母,然后用子字符串增加第一个字母。我假设它是用Java编写的(可能与C相同?) 看看Ascii表() 对于第一个大写字母,生成一个介于65-90之间的整数,然后将其设置为字符。对于小写,生成97-122之间的整数,然后将其设置为字符。将字符合并成一个单词。这感觉有点愚蠢,但我做到了。如果单词必须是真实的,那么你将需要
对于第一个大写字母,生成一个介于65-90之间的整数,然后将其设置为字符。对于小写,生成97-122之间的整数,然后将其设置为字符。将字符合并成一个单词。这感觉有点愚蠢,但我做到了。如果单词必须是真实的,那么你将需要其他东西
import java.util.Random;
public class random {
public static void main(String[] args){
String word1=generateWord();
String word2=generateWord();
String word3=generateWord();
System.out.print(word1+"\n"+word2+"\n"+word3+"\n");
String allThreeWithTheFirstLetterCapital=putThemTogether(word1,word2,word3);
System.out.print(allThreeWithTheFirstLetterCapital);
}
public static String generateWord(){
Random r=new Random();
StringBuilder sb=new StringBuilder();
sb.append((char)(65+r.nextInt(25)));
for(int i=0;i<9;i++){
sb.append((char)(97+r.nextInt(25)));
}
return sb.toString();
}
public static String putThemTogether(String...strings){
StringBuilder sb=new StringBuilder(strings[0]);
for(int i=1;i<strings.length;i++){
sb.append(strings[i].toLowerCase());
}
return sb.toString();}
}
这是:
导入javax.swing.JOptionPane
公共班机{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main();
}
public Main() {
double number1 = Double.parseDouble(JOptionPane.showInputDialog("Input first numnber:"));
double number2 = Double.parseDouble(JOptionPane.showInputDialog("Input second number:"));
String operation = JOptionPane.showInputDialog("Input operation +,-,/,*");
JOptionPane.showMessageDialog(null, number1*2);
JOptionPane.showMessageDialog(null, number2*2);
double result = 0;
if (operation.equals("+")) {
result = number1+number2;
} else if (operation.equals("-")) {
result = number1-number2;
} else if (operation.equals("/")) {
result = number1/number2;
} else if (operation.equals("*")) {
result = number1*number2;
}
}
}没有语言标签-似乎
Java
!欢迎来到堆栈溢出。请阅读并浏览。谢谢你的帮助。我还有一项任务不能完成。它说:制作一个程序,要求用户输入两个数字和一个运算(+、-、/、*)。两个输入的数字都先乘以2,然后用乘法后得到的输入数字执行运算。注意:使用If,and static方法。做这些作业的人在乎我给你代码吗?这里如果你使用这些代码,我想你的老师会知道你作弊了,所以你要明白。使用Scanner(Sytem.in);这对我来说太复杂了。我发布了未完成的作业。你做得很好,代码很好,只需使用if和else,而不是开关,如果字符串对java来说是新的,需要更新的java。哦,我认为你需要在操作之前将a和b乘以2,所以System.out.println(a*2+b*2);我是按照你告诉我的方式做的,使用if和else if,但现在它没有经过运算+,-,/,*。我想让它执行输入操作,但是用2乘法后得到的数字。没关系,我做了。我忘了写System.out.println(“结果是:“+result”);谢谢你的帮助。:)没问题,希望你在班上表现好。祝你好运。
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main();
}
public Main() {
double number1 = Double.parseDouble(JOptionPane.showInputDialog("Input first numnber:"));
double number2 = Double.parseDouble(JOptionPane.showInputDialog("Input second number:"));
String operation = JOptionPane.showInputDialog("Input operation +,-,/,*");
JOptionPane.showMessageDialog(null, number1*2);
JOptionPane.showMessageDialog(null, number2*2);
double result = 0;
if (operation.equals("+")) {
result = number1+number2;
} else if (operation.equals("-")) {
result = number1-number2;
} else if (operation.equals("/")) {
result = number1/number2;
} else if (operation.equals("*")) {
result = number1*number2;
}