如何调整事物 [英] how to align things

查看:68
本文介绍了如何调整事物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里和java..im应该输出一个收据,使价格与右边对齐。我是新手,我只懂基础知识。我得知如何做到这一点:


Strin someString =" Hello there,kenny。" ;;

int numChars = someString.length ();


所以基本上我需要找到一种方法将价格调到右边,记住商品的长度和价格的长度。任何想法都将是非常感谢

hi im new here and to java..im supposed to output a receipt that aligns the prices to the right. Since im new, i only know the basics. I was given a clue on how to do this:

Strin someString = "Hello there, kenny.";
int numChars = someString.length();

so basically i need to find a way to align prices to the right, keeping in mind the length of the item and and length of the price..any ideas will be most appreciated

推荐答案

哦是啊..所以到目前为止我学到的基本上是if语句和循环(当然还有delcaring东西)
oh yeah..so basically wat i learned so far are if statements and loops (and delcaring stuff of course)



哦是啊..所以到目前为止我学到的基本上是if语句和循环(当然还有delcaring东西)
oh yeah..so basically wat i learned so far are if statements and loops (and delcaring stuff of course)



假设您想要在控制台上正确对齐,您可以按如下方式使用字符串的长度。


假设您希望价格出现在60个空格处从控制台左侧


对于每个项目,您可以使用th得到项目名称的长度你在上面给出的方法。


int l = item.length();

然后你得到你需要添加多少空格

int 空格 = 60 - l;


因此,当打印商品item1及其价格时,您打印商品名称,然后打印< b>空格空格,然后打印项目的价格并转到下一行。如果对所有项目执行此操作,则始终会在屏幕左侧对齐60个空格,假设项目名称长度不超过60个字符

Assuming you want to right align things on the console, you can play around with the length of the strings as follows.

Suppose you want the prices to appear at 60 spaces from the left of the console

For each item, you get the length of the item''s name using the method you were give above.

int l = item.length();
Then you get how many spaces you need to add using
int spaces = 60 - l;

So when printing item item1 and its price, You print the item name, then you print spaces spaces, then you print the item''s price and go to the next line. If you do this for all the items, you will always get the prices aligned 60 spaces form the left of the screen assuming the names of the items are not more than 60 characters long


[quote = r035198x]假设你想要在控制台上正确对齐东西,你可以按照以下方式使用字符串的长度。


所以对于输出,wat我准确地说。 .sopln(item + space)
[quote=r035198x]Assuming you want to right align things on the console, you can play around with the length of the strings as follows.

so for the output, wat do i put exactly..s.o.pln (item + space)


这篇关于如何调整事物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆