我在一个文件中有10个图像,我想用10到99之间的随机数添加到现有文件名中来重命名它们。 例如: FileA.jpg > 45FileA.jpg FileB.jpg > 22FileB.jpg 以下是不正确的。 如何在这个函数中将随机整数转换为字符串? ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我是一个初学者,被困在一个我不知道该怎么做的地方。 作业问这个:
1)要求用户输入文件名或“完成”退出
2)增加文件名:添加“_n”
3)向用户询问随机数的上限(最大为100)
4) 向用户询问应写入文件的随机数 (m)
5)创建文件并放置(m)个随机整数(最大值由用户插入,小于100)
6)打开创建的文件,让它读取它并将所有值相加并显示它
7) 循环回#1 8) 当用户输入“done”时,显示在 session 期间创建的文件数
这就是我到目前为止所拥有的:
import javax.swing.JOptionPane;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class Program {
public static void main (String[] args)
throws IOException {
String fileName;
String sum = null;
String upperRangeLimit;
String userRandomNumber;
boolean done = false;
int x = 1;
//int n;
while (!done)
{
fileName = JOptionPane.showInputDialog("Enter a file name or done to exit: ");
if (!(fileName.equals("done")))
fileName = "fileName" + "_" + x++;
Random randomNumbers = new Random(100);
upperRangeLimit = JOptionPane.showInputDialog("Enter the upper range limit:\n Maximum range is 100");
userRandomNumber = JOptionPane.showInputDialog("Error; Maximum range is 100. Enter another number: ");
File file = new File(fileName);
PrintWriter outputFile = new PrintWriter(fileName);
//for (n = 1; n < upperRangeLimit; n++)
//{
// sum =
//}
outputFile.println(randomNumbers);
outputFile.close();`enter code here`
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
sum = inputFile + inputFile.nextLine();
}
System.out.println(sum);
inputFile.close();
if (fileName.equals("done"))
done = true;
}
}
}
非常需要并感谢您的帮助,谢谢!
我稍微更改了您的代码,现在它似乎可以正常工作,就像您希望的那样。 阅读我的评论以了解您做错了什么。
import javax.swing.JOptionPane;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class Program{
public static void main(String[] args) throws IOException {
String fileName;
int sum = 0; // this should be integer because you want to sum the numbers...
String upperRangeLimit;
String userRandomNumber;
boolean done = false;
int x = 1;
// int n;
while (!done) {
fileName = JOptionPane.showInputDialog("Enter a file name or done to exit: ");
if (!(fileName.equals("done")))
fileName = fileName + "_" + x++; // you wrote "fileName" like that which means you dont actually
// reference the variable
// I didnt understand this part , i mean you ask for the input correctly but the text you provide to the user isnt write
upperRangeLimit = JOptionPane.showInputDialog("Enter the upper range limit:\n Maximum range is 100");
userRandomNumber = JOptionPane.showInputDialog("Error; Maximum range is 100. Enter another number: ");
File file = new File(fileName);
// convert user input to integers
int limit = Integer.parseInt(upperRangeLimit);
int range = Integer.parseInt(userRandomNumber);
// write to the file
PrintWriter outputFile = new PrintWriter(fileName);
for (int n = 1; n < limit; n++) {
// (Math.random() * ((max - min) + 1)) + min => way to generate random number in
// range
int random = (int) ((Math.random() * ((range - 1) + 1)) + 1);
outputFile.println(random);
}
outputFile.close();
// read the file
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext()) {
// sum up all of the lines
// the integers are written in the file as strings so you have to convert them
// back to integers again
int nextNum = Integer.parseInt(inputFile.nextLine());
sum = sum + nextNum;
}
System.out.println(sum);
inputFile.close();
if (fileName.equals("done"))
done = true;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.