Java打印包含整数的字符串 [英] Java printing a String containing an integer

查看:60
本文介绍了Java打印包含整数的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此表示怀疑.

public static void main(String[] args) throws IOException{
  int number=1;
  System.out.println("M"+number+1);
}

输出: M11

但是我想打印M2而不是M11.我无法对++进行编号,因为该变量涉及一个for循环,如果这样做,则会给我不同的结果,并且由于输出格式更改,因此无法使用其他print语句进行打印.

But I want to get it printed M2 instead of M11. I couldn't number++ as the variable is involved with a for loop, which gives me different result if I do so and couldn't print it using another print statement, as the output format changes.

请您帮助我如何正确打印它.

Requesting you to help me how to print it properly.

推荐答案

尝试一下:

System.out.printf("M%d%n", number+1);

其中%n 是换行符

这篇关于Java打印包含整数的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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