我有一个遍历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库,您可以从我的Dropbox中获取:http://db.tt/H8zmyA75
答案 0 :(得分:0)
这就是发明阵列的原因。将每对字符放入
char[][] pairs;
或者更好的
ArrayList<char[]> pairs;
如果你无法提前预测多少。
答案 1 :(得分:0)
import java . util . * ;
//parse strings to integers
public int convertStringToInt(String stringOfDigits)
{
int number = 0;
int total = 0;
String subData = "";
List<Integer> numbers = new ArrayList<>();
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
numbers . add ( Integer.parseInt(subData) ) ; //converts the substring to an integer
}
}
答案 2 :(得分:0)
根据我对您的问题的理解,您希望根据字符串的大小创建多个变量。并且字符串中的每个两个字符代表一个整数。 数组可以描述为变量列表。 因此,如果您有一个10个字符的字符串,那么这10个字符代表5个整数。 所以你需要一个长度 5的数组(数组中的变量数或'单元')
创建整数数组:
int[] arrayName = new int[SIZE];
您还将返回数组并将其作为变量“列表”处理
例如:
public static int [] convertStringToInteger(String toConvert) {
int [] toReturn = new int[toConvert.length()/2];
//Assuming passed strings are of even length
for (int i = 0, j = 0; i < toConvert.length() || j < toReturn.length; i +=2 , j++) {
String temp = toConvert.charAt(i) + "" + toConvert.charAt(i+1);
toReturn [j] = Integer.parseInt(temp);
}
return toReturn;
}
数组很容易使用,并且理解你可以通过oracle here
检查Java教程中的数组希望这有助于。