Java 有没有办法打印一个变量,修改同一个变量,并在列表中打印这两个变量?
首先,如果这是一个比较复杂的问题,我很抱歉,但是我不知道我在编码方面到底想做什么 为了澄清我想做什么,我想知道一种方法,将Java 有没有办法打印一个变量,修改同一个变量,并在列表中打印这两个变量?,java,loops,variables,user-input,Java,Loops,Variables,User Input,首先,如果这是一个比较复杂的问题,我很抱歉,但是我不知道我在编码方面到底想做什么 为了澄清我想做什么,我想知道一种方法,将收据的值“保存”到列表/集合/数组中,然后通过确定收据的值的过程再次保存到列表中,当我打印列表时,两个不同的收据值会依次打印。 假设第一次确定receipt的值时,它等于x,然后在我将其保存到列表中并通过相同的值确定过程receipt=y之后,我将其添加到列表中。它会打印:x,然后是y。还是一对一对?如果它两次打印收据的新值,如何使它在两次之后打印这两个不同的值 import
收据的值“保存”到列表/集合/数组中,然后通过确定收据的值的过程再次保存到列表中,当我打印列表时,两个不同的收据值会依次打印。
假设第一次确定receipt
的值时,它等于x,然后在我将其保存到列表中并通过相同的值确定过程receipt
=y之后,我将其添加到列表中。它会打印:x,然后是y。还是一对一对?如果它两次打印收据的新值
,如何使它在两次之后打印这两个不同的值
import java.util.Scanner;
public class TicketV005
{ // start of public class
public static void main (String [] args)
{ // start of main
Scanner keyboard = new Scanner(System.in);
int printedTickets,ticketTotal;
String fareType, purchaseLoc;
String answer1 =("null");
String receipt =("null");
int zoneAmount = 0;
double price = 0;
int answer2 = 0;
System.out.print("How many tickets do you wish to buy?(Answer with a number please.) ");
ticketTotal = keyboard.nextInt();
printedTickets = 0;
while (ticketTotal <= 0)
{
System.out.println("\nIncorrect input, try again.");
System.out.println("How many tickets do you wish to buy?(Answer with a number please.) ");
ticketTotal = keyboard.nextInt();
}
while(ticketTotal !=printedTickets )
{
System.out.print("Welcome! Are you buying a reduced fare ticket?(y/n) ");
answer1 = keyboard.next();
if (answer1.equals("y"))
fareType=("reduced fare");
else if (answer1.equals("n"))
fareType=("standard fare");
else
fareType=("null");
while (fareType.equals("null"))
{
System.out.println("\nIncorrect input, try again.");
System.out.println("Welcome! Are you buying a reduced fare ticket?(y/n) ");
answer1 = keyboard.next();
}
System.out.println("Would you like to purchase your ticket at 1. the automated services or at 2. the cashier? ");
answer2 = keyboard.nextInt();
if (answer2 ==1)
purchaseLoc=("automated services");
else if (answer2 ==2)
purchaseLoc=("cashier");
else
purchaseLoc=("null");
while (purchaseLoc.equals("null"))
{
System.out.println("\nIncorrect input, try again.");
System.out.println("Would you like to purchase your ticket at 1. the automated services or at 2. the cashier? ");
answer2 = keyboard.nextInt();
}
System.out.println("How many zones will you be travelling? (1-3) ");
zoneAmount = keyboard.nextInt();
while (zoneAmount <= 0 || zoneAmount > 3 )
{
System.out.println("\nIncorrect input, try again.");
System.out.println("How many zones will you be travelling? (1-3) ");
zoneAmount = keyboard.nextInt();
}
//Start of reduced fare
if (answer1.equals("y") && answer2 == 1 )
{ // Start of automated services reduced fare
for (int i= 1 ; i <= 3 ; i++)
{
if (zoneAmount == i)
price=(10*i)+10;
}
} //end off automated services reduced fare
if (answer1.equals("y") && answer2 == 2)
{ // Start of cashier reduced fare
for (int i= 1 ; i <=3 ; i++)
{
if (zoneAmount == i)
price=(14*i)+14;
}
} //End of cashier reduced fare
//End of reduced fare
//Start of standard fare
if (answer1.equals("n") && answer2==1)
{ //Start of standard fare automated services
for (int i = 1; i <=3 ; i++)
{
if ( zoneAmount ==i)
price=(18*i)+18;
}
} // end of standard fare automated servies
if (answer1.equals("n") && answer2==2)
{ // Start of standard fares cashier
for (int i = 1; i <=3 ; i++)
{
if( zoneAmount == i)
price=(22*i)+22;
}
} // End of standard fares cashier
//End of standard fare
System.out.println("");
receipt = (zoneAmount+" zone(s), "+ fareType+", bought at: "+ purchaseLoc+". Your price: "+price+" SEK.");
System.out.println(receipt);
printedTickets++;
System.out.println(""); //Empty line for when/if it repeats
} // end of while printedTickets is not equal to ticketTotal
}// end of main
}// end of public class
import java.util.Scanner;
公共类票证TV005
{//公共类的开始
公共静态void main(字符串[]args)
{//main的开始
扫描仪键盘=新扫描仪(System.in);
int打印的tickets,ticketTotal;
字符串fareType,purchaseLoc;
字符串answer1=(“null”);
字符串收据=(“空”);
int zoneAmount=0;
双倍价格=0;
int answer2=0;
系统输出打印(“您想买多少张票?(请用数字回答”);
ticketTotal=keyboard.nextInt();
printedTickets=0;
虽然(ticketTotal您可以使用a来附加新字符串。或者,您可以简单地使用+
-运算符连接两个字符串,但这在执行时间方面确实效率低下。看起来您在问两个问题。一个问题与是否可以保存变量有关,另一个问题与如何拥有一组变量有关从你的代码中不清楚你的目标是什么
在java中,您声明的东西
String receipt
是引用。这意味着它们跟踪一段数据。但是,其他东西也可以跟踪同一段数据。因此:
String receipt1 = "100EUR";
String receipt2 = receipt1;
在上面的代码中,只有一个内存中有“100EUR”的东西,receipt1和receipt2都在查看它。如果有什么东西出现并修改了其中一个
receipt1 = "200EUR";
另一个不会受到影响
System.out.println(receipt1);
System.out.println(receipt2);
// output would be
200EUR
100EUR
在很多情况下,一旦某个引用通过=符号分配给某个引用,您可以想象该引用总是可以访问它,直到下一次使用=符号分配该引用指向其他引用时才会更改。但是,在Java中,对象可以更改状态,因此如果
List<String> myList = new ArrayList<String>():
首先,可以声明字符串的ArrayList:
ArrayList<String> receipts = new ArrayList<>();
最后,您可以打印ArrayList的所有元素
for (String s : receipts)
System.out.println(s);
我会使用StringBuilder,继续在循环中添加所需内容,并在循环结束时向其添加新行:
public static void main(String[] args)
{
StringBuilder receipt;
//Other variable initializations
此外,您的while循环可能会更改为for循环:
for (int i = 0; i < ticketTotal; i++)
{
receipt.append(zoneAmount);
receipt.append(" zone(s), ");
receipt.append(fareType);
receipt.append(", bought at: ");
receipt.append(purchaseLoc);
receipt.append(". Your price: ");
receipt.append(price);
receipt.append(" SEK.\n");
}
StringBuilder在内部用于执行静态连接,即读取字符串a=“b”+“c”+“d”的代码
stuff,一般来说这是一个好主意。动态使用+并不完美,但除非你正在承受巨大的负载,否则它不会杀死你。这只是一个相当弱的实践。这正是我想做的,但一旦Receipt值的值添加到ArrayList中,我可以使用该方法通过确定val的过程吗使用receipt,然后将receipt的新值添加到ArrayList中,而不会更改添加到列表中的第一个receipt值?编辑:拼写。很抱歉,我不太明白您需要什么。如果您给我一个具体的示例,我很乐意提供帮助。另外,我在某处看到一篇关于制作列表的帖子,用户建议使用Set函数相反,如果启用,您会判断哪一个最有效?看起来您希望每个收据值在最后输出时被记住。如果您使用列表,它们将按顺序被记住。集合将记住它们,但以集合选择的任何顺序存储它们。即使您重新定义收据
,您存储的收据根据我关于参考资料的解释,该列表将不受影响。
public static void main(String[] args)
{
StringBuilder receipt;
//Other variable initializations
for (int i = 0; i < ticketTotal; i++)
{
receipt.append(zoneAmount);
receipt.append(" zone(s), ");
receipt.append(fareType);
receipt.append(", bought at: ");
receipt.append(purchaseLoc);
receipt.append(". Your price: ");
receipt.append(price);
receipt.append(" SEK.\n");
}
System.out.println(receipt.toString());