我有一个遍历String
的for循环,每次取出2个字符并将它们分配给一个变量。我编写的代码段如下:
//parse strings to integers
public int convertStringToInt(String stringOfDigits)
{
int number = 0;
int total = 0;
String subData = "";
for(int n = 0; n < stringOfDigits.length() ; n+=2) //For loop to pick out 2 digit sets of numbers from a string literal
{
subData = stringOfDigits.substring(n, n+2); //pulls a 2 digit substring and assigns it to a variable
number = Integer.parseInt(subData); //converts the substring to an integer
}
}
我需要以某种方式将前两个字符返回到变量(例如var1
),将接下来的两个字符返回到另一个变量(var2
),依此类推。然后我需要返回所有这些,以便我可以将其带入我的主模块。
任何帮助将不胜感激!
编辑:
这是我的整个程序。我得到的答案对于我认为需要的来说相当复杂。也许更简单的解决方案在其他地方。该程序打印出一个条形图,显示有多少用户使用我所知道的每个操作系统(linux、mac、windows)。 String
包含这些数字,我需要将这些单独的两个数字对放入 drawBar()
方法中,以便它可以绘制包含那么多用户的条形图。
/**
* The purpose of this program is to draw a bar graph using turtle graphics
* and object oriented programming techniques based on information from a survey on
* Operating System use.
*
* @author Andrew Hauser (Shibumi)
* @version 1/16/12
*/
import java.awt.*; //imports the awt module
class Survey //Declares the Survey class
{
//Draws a line (or rectangle) from the first set of coordinates to the second set of coordinates.
public static void drawLine(Turtle myrtle, Color color, int penWidth, int x1, int y1, int x2, int y2)
{
myrtle.hide();
myrtle.penUp();
myrtle.setPenColor(color);
myrtle.setPenWidth(penWidth);
myrtle.moveTo(x1, y1);
myrtle.penDown();
myrtle.moveTo(x2, y2);
}//end of method
//Draws the box that the graph is contained in
public static void drawBox(Turtle myrtle, Color color)
{
myrtle.hide();
myrtle.penUp();
myrtle.setPenColor(color);
myrtle.setPenWidth(1);
myrtle.moveTo(100, 10);
myrtle.penDown();
myrtle.moveTo(400, 10);
myrtle.moveTo(400, 250);
myrtle.moveTo(100, 250);
myrtle.moveTo(100, 10);
myrtle.penUp();
}
//Draws the value lines on the outside of the box
public static void drawBoxLines(Turtle myrtle, Color color)
{
myrtle.hide();
myrtle.penUp();
myrtle.setPenWidth(1);
myrtle.moveTo(100, 250);
for(int nums = 0; nums <=10; nums++)
{
myrtle.turnLeft();
myrtle.penDown();
myrtle.forward(10);
myrtle.penUp();
myrtle.backward(10);
myrtle.turnRight();
myrtle.forward(20);
}
}
//Draws a bar for the graph
public static void drawBar(Turtle myrtle, Color color, int value, int x1, int y1)
{
myrtle.hide();
myrtle.penUp();
myrtle.setPenColor(color);
myrtle.setPenWidth(1);
myrtle.moveTo(x1, y1);
myrtle.penDown();
myrtle.forward(value * 10);
myrtle.turnRight();
myrtle.forward(10);
myrtle.turnRight();
myrtle.forward(value * 10);
myrtle.penUp();
myrtle.turnLeft();
myrtle.turnLeft();
}
//parse strings to integers
public int convertStringToInt(String stringOfDigits)
{
int number = 0;
int total = 0;
String subData = "";
for(int n = 0; n < stringOfDigits.length() ; n+=2) //For loop to pick out 2 digit sets of numbers from a string literal
{
subData = stringOfDigits.substring(n, n+2); //pulls a 2 digit substring and assigns it to a variable
number = Integer.parseInt(subData); //converts the substring to an integer
}
}
}
public class SurveyTester //Declares the SurveyTester class
{
//Executes the code.
public static void main(String[] args) //Start of main method
{
World worldObj = new World(); //Makes a new world
Turtle myrtle = new Turtle(0, 0, worldObj); //Makes a new Turtle object
Picture pictureObj = new Picture("Graph_background.png");
worldObj.setPicture(pictureObj);
Survey survey = new Survey(); //Makes a new Survey object
stringData = "031016";
survey.drawBox(myrtle, Color.BLACK);
survey.drawBoxLines(myrtle, Color.BLACK); //Draws lines by twos
survey.drawBar(myrtle, Color.RED, 3, 150, 250); //Bar for linux
survey.drawBar(myrtle, Color.RED, 10, 240, 250); //Bar for Mac
survey.drawBar(myrtle, Color.RED, 16, 330, 250); //Bar for windows
}
}
非常感谢您的帮助!
PS。如果您想实际运行看看它是什么样子,您将需要 bookClasses Java 库,您可以从我的保管箱中获取它:http://db.tt/H8zmyA75最佳答案
这就是发明数组的原因。将每对字符放入
char[][] pairs;
或者也许更好
ArrayList<char[]> pairs;
如果您无法提前预测有多少。
关于java - 每次循环迭代时,我可以将不同的信息从 for 循环返回到新变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8888786/