wwwxinyu1990
发表于 2015-1-28 12:41:55
wwwxinyu1990 发表于 2015-1-27 12:32
第八题求指教,知道原因,却想不到解决办法~
谢谢!我去看看{:1_1:}
python001
发表于 2015-1-29 09:50:11
支持一下
曾经的肆无忌惮
发表于 2015-1-30 16:17:49
/*
No01:程序设计,写一个程序验证一个整数是否是奇数
*/
class Odd {
public static void main(String[] args) {
int n = 1;
if (n % 2 == 1)
System.out.print(n+"是奇数");
else
System.out.print(n+"是偶数");
}
}
/*
No02:程序改错,计算1 - 20 的累加
*/
public class Test {
public static void main(String args[]){
int t = 0;
for(int i=0 ;i <= 20;i++){
t = t + i;
}
System.out.println(t);
}
}
/*
No03:程序设计,输出倒立三角形 (5分)
*/
class Triangle {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(' ');
}
for (int j = 5; j > i; j--) {
System.out.print("* ");
}
System.out.println();
}
}
}
/*
No04:程序改错,输出minutes = 60
*/
public class Clock {
public static void main(String[] args) {
int minutes = 0;
for(int ms =0; ms < 60*60*1000; ms ++){
if(ms%(60*1000) == 0){
minutes ++;
}
}
System.out.println(" minutes = "+minutes);
}
}
/*
No05:程序设计,实现字符串反转,例如输入“i love fishc" , "应输出chsif evol i"
*/
class Reversal {
public static void main(String[] args) {
String n = "i love fishc";
char[] ch = n.toCharArray();
for (int i = ch.length - 1; i >= 0; i--) {
System.out.print(ch);
}
}
}
/*
No06:程序改错
*/
public class DoubleSub {
public static void main(String[] args) {
System.out.println((float)(2.0)- (float)(1.1));
}
}
/*
No09:程序设计,x = 2014 ,y = 2015,用异或交换x , y的值
*/
class Exchange {
public static void main(String[] args) {
int x = 2014;
int y = 2015;
System.out.println("x = "+x+",y = "+y);
x = x^y;
y = x^y;
x = x^y;
System.out.println("x = "+x+",y = "+y);
}
}
/*
No10:程序设计,用最有效率的方法算出2乘以8等於几
*/
class Calculation {
public static void main(String[] args) {
System.out.println(2*8);
System.out.println(2<<3); //高效率
}
}
{:9_240:}{:9_240:}{:9_240:}刚开始学还有很多不懂,7、8题没什么思路写来着,不知道这样写可不可以,不要见笑哈
曾经的肆无忌惮
发表于 2015-1-31 21:02:49
第八题是需要考虑他的Unicode编码是吗?
/*
No08:程序设计,打印字符串长度,"a\u0022.length()+\u0022b"
*/
class PrintTest {
public static void main(String[] args) {
String n = "a\u0022.length()+\u0022b";
System.out.println(n.length());
}
}
花生
发表于 2015-2-8 21:33:26
No01
public class _1_1
{
public static void main(String[] args)
{
int test1 = 0;
System.out.println(test1 + "是否是奇数:"+isOdd(test1));
int test2 = 1;
System.out.println(test2 + "是否是奇数:"+isOdd(test2));
int test3 = 2;
System.out.println(test3 + "是否是奇数:"+isOdd(test3));
int test4 = -1;
System.out.println(test4 + "是否是奇数:"+isOdd(test4));
int test5 = -2;
System.out.println(test5 + "是否是奇数:"+isOdd(test5));
}
public static boolean isOdd(int number)
{
//不能被2整除是奇数
return (number & 1) != 0;
}
}
花生
发表于 2015-2-8 21:36:58
No02:程序改错,计算1 - 20 的累加
package 小甲鱼活动;
public class _1_2
{
public static void main(String[] args)
{
int t = 0;//改正一
for (int i = 1; i <= 20; i++)//改正二
{
t = t + i;
}
System.out.println(t);
}
}
花生
发表于 2015-2-8 21:37:39
No03:程序设计,输出倒立三角形
package 小甲鱼活动;
public class _1_3
{
//输出倒立三角形
public static void main(String[] args)
{
int bian = 11;
for (int i = 0; i <= bian/2; i++)
{
for(int k=0;k<i;k++){
System.out.print(" ");
}
for (int j = 0; j < bian - i*2; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
花生
发表于 2015-2-8 21:38:10
No04:程序改错,输出minutes = 60
package 小甲鱼活动;
public class _1_4
{
public static void main(String[] args)
{
int minutes = 0;
for (int ms = 0; ms < 60 * 60 * 1000; ms++)
{
if (ms % (60 * 1000) == 0)//加括号
{
minutes++;
}
}
System.out.println(" minutes = " + minutes);
}
}
花生
发表于 2015-2-8 21:38:50
No05:程序设计,实现字符串反转,例如输入“i love fishc" , "应输出chsif evol i"
package 小甲鱼活动;
import java.util.Scanner;
public class _1_5
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
StringBuffer inputString = new StringBuffer(input);
System.out.print(inputString.reverse());
}
}
花生
发表于 2015-2-8 21:39:25
No06:程序改错两数相减,输出0.9
package 小甲鱼活动;
import java.math.BigDecimal;
public class _1_6
{
public static void main(String[] args)
{
System.out.println(new BigDecimal("2.0")
.subtract(new BigDecimal("1.1")));
}
}
花生
发表于 2015-2-8 21:40:09
No07:程序设计,有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数
package 小甲鱼活动;
import java.util.HashMap;
import java.util.Map;
public class _1_7
{
public static void main(String[] args)
{
String str = "甲鱼是23不是4¥¥鱼@#3";
//System.out.println(str.length());
Map<Character, Integer> result = new HashMap<>();
int length = str.length();
for (int i = 0; i < length; i++)
{
char ch = str.charAt(i);
if (result.containsKey(ch))
{
result.put(ch, result.get(ch) + 1);
}
else
result.put(ch, 1);
}
for (char c : result.keySet())
{
System.out.println(c + " 有 " + result.get(c) + "个");
}
}
}
花生
发表于 2015-2-8 21:40:43
No07:程序设计,有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数
package 小甲鱼活动;
import java.util.HashMap;
import java.util.Map;
public class _1_7
{
public static void main(String[] args)
{
String str = "甲鱼是23不是4¥¥鱼@#3";
//System.out.println(str.length());
Map<Character, Integer> result = new HashMap<>();
int length = str.length();
for (int i = 0; i < length; i++)
{
char ch = str.charAt(i);
if (result.containsKey(ch))
{
result.put(ch, result.get(ch) + 1);
}
else
result.put(ch, 1);
}
for (char c : result.keySet())
{
System.out.println(c + " 有 " + result.get(c) + "个");
}
}
}
花生
发表于 2015-2-8 21:41:16
No08:程序设计,打印字符串长度,"a\u0022.length()+\u0022b"
package 小甲鱼活动;
public class _1_8
{
public static void main(String[] args)
{
String str = "a\\u0022.length() + \\u0022b";
System.out.println(str.length());
}
}
花生
发表于 2015-2-8 21:42:08
No09:程序设计,x = 2014 ,y = 2015,用异或交换x , y的值
package 小甲鱼活动;
public class _1_9
{
public static void main(String[] args)
{
int x = 2014, y = 2015;
x = x ^ y;
y = y ^ x;
x = y ^ x;
System.out.println("x=" + x);
System.out.println("y=" + y);
}
}
花生
发表于 2015-2-8 21:42:45
No10:程序设计,用最有效率的方法算出2乘以8等於几
package 小甲鱼活动;
public class _1_10
{
public static void main(String[] args)
{
//口算最快
System.out.println("2*8 = " + 16);
//口算不行的话,就这样吧
System.out.println("2*8 = " + (2 << 3));
}
}
花生
发表于 2015-2-8 21:45:14
N07我不小心重复提交了一次。两次是一样的。知道了吗楼主?
月之吟
发表于 2015-2-17 17:59:33
本帖最后由 月之吟 于 2015-2-17 18:18 编辑
No 01import java.util.Scanner;
public class No01 {
public static void main(String[] args) {
int i;
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要判断的整数:");
i = sc.nextInt();
if (i % 2 != 0) {
System.out.println("该整数是奇数");
} else {
System.out.println("该整数不是奇数");
}
sc.close();
}
}
No 02
for循环改为for(int i = 1; i <= 20; i++),把 t+i 强制转型为 short。把 t 定义成 int 型也行吧。
public class No02 {
public static void main(String args[]) {
short t = 0;
for (int i = 1; i <= 20; i++) {
t = (short) (t + i);
}
System.out.println(t);
}
}
No 03
public class No03 {
public static void main(String[] args) {
int num = 5;
for (int i = 0; i < num; i++) {
for (int m = 0; m < i; m++) {
System.out.print(" ");
}
for (int n = 0; n < (num - i) * 2 - 1; n++) {
System.out.print("*");
}
System.out.println();
}
}
}
No 04
将 if 中的60*1000用括号括起来。
public class No04 {
public static void main(String[] args) {
int minutes = 0;
for (int ms = 0; ms < 60 * 60 * 1000; ms++) {
if (ms % (60 * 1000) == 0) {
minutes++;
}
}
System.out.println(" minutes = " + minutes);
}
}
No 05
import java.util.Scanner;
public class No05_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要反转的字符串:");
String s1 = sc.nextLine();
String s2 = new String();
for (int i = s1.length() - 1; i >= 0; i--) {
s2 += s1.charAt(i);
}
System.out.println("反转后的字符串为:\n" + s2);
sc.close();
}
}
上面不知道一个字符一个字符加到s2上是不是不好……就又写了下面的……
import java.util.Scanner;
public class No05_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要反转的字符串:");
String s1 = sc.nextLine();
char[] ch = new char;
for (int i = 0; i < s1.length(); i++) {
ch = s1.charAt(s1.length() - i - 1);
}
String s2 = new String();
s2 = String.valueOf(ch);
System.out.println("反转后的字符串为:\n" + s2);
sc.close();
}
}
不过如果直接用StringBuilder的reverse方法应该不算吧……
No 06
2.0和1.1后面都加f。只会这么改了- -
public class No06 {
public static void main(String[] args) {
System.out.println(2.0f - 1.1f);
}
}
No 07
import java.util.Scanner;
public class No07 {
public static void main(String[] args) {
int word = 0, num = 0, cn = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要统计的字符串:");
String s = sc.nextLine();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 48 && s.charAt(i) <= 57) {
num++;
}
if (s.charAt(i) >= 65 && s.charAt(i) <= 90) {
word++;
}
if (s.charAt(i) >= 97 && s.charAt(i) <= 122) {
word++;
}
if (s.charAt(i) >= 19968 && s.charAt(i) <= 40869) {
cn++;
}
}
System.out.println("有" + num + "个数字");
System.out.println("有" + word + "个英文");
System.out.println("有" + cn + "个汉字");
sc.close();
}
}
No 08
import java.util.Scanner;
public class No08 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String s = sc.nextLine();
System.out.println(s + "字符串的长度为" + s.length());
sc.close();
System.out.println("a\\u0022.length()+\\u0022b的字符串长度为");
System.out.println("a\\u0022.length()+\\u0022b".length());
}
}
上面这个的"a\u0022.length()+\u0022b"的长度也只会这么着了- -
No 09
public class No09 {
public static void main(String[] args) {
int x = 2014, y = 2015;
System.out.println("交换前的值为:x=" + x + " y=" + y);
x = x ^ y;
y = x ^ y;
x = x ^ y;
System.out.println("交换后的值为:x=" + x + " y=" + y);
}
}
No 10
public class No10 {
public static void main(String[] args) {
System.out.println("2*8=" + (2 << 3));
}
}
有错或者不好也不要笑话我哦~{:5_109:}