⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5.txt

📁 这种重定向的方法
💻 TXT
📖 第 1 页 / 共 3 页
字号:
var re=/^\d{1,8}$|\.\d{1,2}$/;
var str=document.form1.all(i).value;
var r=str.match(re);
if (r==null)
{
   sign=-4;
   break;
}
else{
   document.form1.all(i).value=parseFloat(str);
} 
 
148 、 将一个键盘输入的数字转化成中文输出 
(例如:输入:1234567     输出:一百二拾三万四千五百六拾七)
用java语言实现,,请编一段程序实现!
 public class Reader {
  private String strNum;
  private String strNumChFormat;
  private String strNumTemp;
  private int intNumLen;
  private String strBegin;
  public Reader(String strNum) {
    this.strNum = strNum;
  }
  public boolean check(String strNum) {
    boolean valid = false;
    
    if (strNum.substring(0,1).equals("0")){
     this.strNum = strNum.substring(1);
    }
    try {
      new Double(strNum);
      valid = true;
    }
    catch (NumberFormatException ex) {
      System.out.println("Bad number format!");
    }
    return valid;
  }
  public void init() {
    strNumChFormat = "";
    intNumLen = strNum.length();
    strNumTemp = strNum;
    strNumTemp = strNumTemp.replace('1', '一');
    strNumTemp = strNumTemp.replace('2', '二');
    strNumTemp = strNumTemp.replace('3', '三');
    strNumTemp = strNumTemp.replace('4', '四');
    strNumTemp = strNumTemp.replace('5', '五');
    strNumTemp = strNumTemp.replace('6', '六');
    strNumTemp = strNumTemp.replace('7', '七');
    strNumTemp = strNumTemp.replace('8', '八');
    strNumTemp = strNumTemp.replace('9', '九');
    strNumTemp = strNumTemp.replace('0', '零');
    strNumTemp = strNumTemp.replace('.', '点');
    strBegin = strNumTemp.substring(0, 1);
  }
  public String readNum() {
    if (check(strNum)) {
      init();
      try {
        for (int i = 1, j = 1, k = 1; i < intNumLen; i++) {
          if (strNumTemp.charAt(intNumLen - 1) == '零' && i == 1) {
            strNumChFormat = "位";
          }
          else if (strNumTemp.charAt(intNumLen - i) == '零' && j == 1) {
            strNumChFormat = "位" + strNumChFormat;
          }
          else if (strNumTemp.charAt(intNumLen - i) == '点') {
            j = 1;
            k = 1;
            strNumChFormat = strNumTemp.charAt(intNumLen - i) + strNumChFormat;
            continue;
          }
          else {
            strNumChFormat = strNumTemp.charAt(intNumLen - i) + strNumChFormat;
          }
          if (strNumTemp.charAt(intNumLen - i - 1) != '位' &&
              strNumTemp.charAt(intNumLen - i - 1) != '零') {
            if (j == 1 && i < intNumLen) {
              strNumChFormat = '拾' + strNumChFormat;
            }
            else if (j == 2 && i < intNumLen) {
              strNumChFormat = '百' + strNumChFormat;
            }
            else if (j == 3 && i < intNumLen) {
              strNumChFormat = '千' + strNumChFormat;
            }
          }
          if (j == 4 && i < intNumLen) {
            j = 0;
          }
          if (k == 4 && i < intNumLen) {
            strNumChFormat = '万' + strNumChFormat;
          }
          else if (k == 8 && i < intNumLen) {
            k = 0;
            strNumChFormat = '亿' + strNumChFormat;
          }
          j++;
          k++;
        }
        while (strNumChFormat.indexOf("位") != -1) {
          strNumChFormat = strNumChFormat.replaceAll("位", " ");
        }
        if (strNumChFormat.substring(0, 2) == "一拾") {
          strNumChFormat = strNumChFormat.substring(1, strNumChFormat.length());
        }
        if (strNumChFormat.indexOf("点") >= 0) {
          String rebegin = strNumChFormat.substring(0,
              strNumChFormat.indexOf("点"));
          String relast = strNumChFormat.substring(strNumChFormat.indexOf("点"),
              strNumChFormat.length());
          for (int i = 1; i <= relast.length(); i++) {
            relast = relast.replaceAll("拾", "");
            relast = relast.replaceAll("百", "");
            relast = relast.replaceAll("千", "");
            relast = relast.replaceAll("万", "");
            relast = relast.replaceAll("亿", "");
          }
          strNumChFormat = rebegin + relast;
        }
      }
      catch (ArrayIndexOutOfBoundsException ex) {
        ex.printStackTrace();
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
      int off = strNumChFormat.indexOf("点");
      strNumChFormat = strBegin + strNumChFormat.substring(0);
    }
    else {
      strNumChFormat = "";
    }
    return strNumChFormat;
  }
  public static void main(String args[]) {
    try {
      String number = args[0].toString();
      System.out.println("The number is: " + number);
      Reader reader = new Reader(number);
      System.out.println("Output String: " + reader.readNum());
    }
    catch (Exception ex) {
      System.out.println("Please input like that: javac Reader <number>");
    }
  }
} 
 
149 、 JAVA 代码查错 
1.
abstract class Name {
   private String name;
   public abstract boolean isStupidName(String name) {}
}
大侠们,这有何错误 ?
答案 :  错。 abstract method 必须以分号结尾,且不带花括号。 
2.
public class Something {
   void doSomething () {
       private String s = "";
       int l = s.length();
   }
}
有错吗 ?
答案 :  错。局部变量前不能放置任何访问修饰符  (private , public ,和 protected) 。 final 可以用来修饰局部变量 
(final 如同 abstract 和 strictfp ,都是非访问修饰符, strictfp 只能修饰 class 和 method 而非 variable) 。 
3.
abstract class Something {
   private abstract String doSomething ();
}
这好像没什么错吧 ?
答案 :  错。 abstract 的 methods 不能以 private 修饰。 abstract 的 methods 就是让子类 implement( 实现 ) 具体细节的,怎么可以用 private 把 abstract
method 封锁起来呢 ? ( 同理, abstract method 前不能加 final) 。 
4.
public class Something {
   public int addOne(final int x) {
       return ++x;
   }
}
这个比较明显。 
答案 :  错。 int x 被修饰成 final ,意味着 x 不能在 addOne method 中被修改。 
5.
public class Something {
   public static void main(String[] args) {
       Other o = new Other();
       new Something().addOne(o);
   }
   public void addOne(final Other o) {
       o.i++;
   }
}
class Other {
   public int i;
}
和上面的很相似,都是关于 final 的问题,这有错吗 ?
答案 :  正确。在 addOne method 中,参数 o 被修饰成 final 。如果在 addOne method 里我们修改了 o 的 reference
( 比如 : o = new Other();) ,那么如同上例这题也是错的。但这里修改的是 o 的 member vairable
( 成员变量 ) ,而 o 的 reference 并没有改变。 
6.
class Something {
    int i;
    public void doSomething() {
        System.out.println("i = " + i);
    }
} 
有什么错呢 ?  看不出来啊。 
答案 :  正确。输出的是 "i = 0" 。 int i 属於 instant variable ( 实例变量,或叫成员变量 ) 。 instant variable 有 default value 。 int 的 default value 是 0 。 
7.
class Something {
    final int i;
    public void doSomething() {
        System.out.println("i = " + i);
    }
}
和上面一题只有一个地方不同,就是多了一个 final 。这难道就错了吗 ?
答案 :  错。 final int i 是个 final 的 instant variable ( 实例变量,或叫成员变量 ) 。 final 的 instant variable 没有 default value ,必须在 constructor ( 构造器 ) 结束之前被赋予一个明确的值。可以修改为 "final int i = 0;" 。 
8.
public class Something {
     public static void main(String[] args) {
        Something s = new Something();
        System.out.println("s.doSomething() returns " + doSomething());
    }
    public String doSomething() {
        return "Do something ...";
    }
}
  看上去很完美。 
答案 :  错。看上去在 main 里 call doSomething 没有什么问题,毕竟两个 methods 都在同一个 class 里。但仔细看, main 是 static 的。 static method 不能直接 call non-static methods 。可改成 "System.out.println("s.doSomething() returns " + s.doSomething());" 。同理, static method 不能访问 non-static instant variable 。 
9.
此处, Something 类的文件名叫 OtherThing.java
class Something {
    private static void main(String[] something_to_do) {        
        System.out.println("Do something ...");
    }
}
  这个好像很明显。 
答案 :  正确。从来没有人说过 Java 的 Class 名字必须和其文件名相同。但 public class 的名字必须和文件名相同。 
10 . 
interface  A{
   int x = 0;
}
class B{
   int x =1;
}
class C extends B implements A {
   public void pX(){
      System.out.println(x);
   }
   public static void main(String[] args) {
      new C().pX();
   }
}
答案:错误。在编译时会发生错误 ( 错误描述不同的 JVM 有不同的信息,意思就是未明确的 x 调用,两个 x 都匹配(就象在同时 import java.util 和 java.sql 两个包时直接声明 Date 一样)。对于父类的变量 , 可以用 super.x 来明确,而接口的属性默认隐含为  public static final. 所以可以通过 A.x 来明确。 
11.
interface Playable {
    void play();
}
interface Bounceable {
    void play();
}
interface Rollable extends Playable, Bounceable {
    Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
    private String name;
    public String getName() {
        return name;
    }
    public Ball(String name) {
        this.name = name;        
    }
   public void play() {
        ball = new Ball("Football");
        System.out.println(ball.getName());
    }
}
这个错误不容易发现。 
答案 :  错。 "interface Rollable extends Playable, Bounceable" 没有问题。 interface 可继承多个 interfaces ,所以这里没错。问题出在 interface Rollable 里的 "Ball ball = new Ball("PingPang");" 。任何在 interface 里声明的 interface variable ( 接口变量,也可称成员变量 ) ,默认为 public static final 。也就是说 "Ball ball = new Ball("PingPang");" 实际上是 "public static final Ball ball = new Ball("PingPang");" 。在 Ball 类的 Play() 方法中, "ball = new Ball("Football");" 改变了 ball 的 reference ,而这里的 ball 来自 Rollable interface , Rollable interface 里的 ball 是 public static final 的, final 的 object 是不能被改变 reference 的。因此编译器将在 "ball = new Ball("Football");" 这里显示有错。 
28 、设计 4 个线程,其中两个线程每次对 j 增加 1 ,另外两个线程对 j 每次减少 1 。写出程序。 
以下程序使用内部类实现线程,对 j 增减的时候没有考虑顺序问题。 
public class ThreadTest1{
  private int j;
  public static void main(String args[]){
ThreadTest1 tt=new ThreadTest1();
Inc inc=tt.new Inc();
Dec dec=tt.new Dec();
for(int i=0;i<2;i++){
Thread t=new Thread(inc);
t.start();
t=new Thread(dec);
t.start();
}
}
  private synchronized void inc(){
j++;
System.out.println(Thread.currentThread().getName()+"-inc:"+j);
  }
  private synchronized void dec(){
j--;
System.out.println(Thread.currentThread().getName()+"-dec:"+j);
  }
  class Inc implements Runnable{
public void run(){
for(int i=0;i<100;i++){
inc();
}
}
  }
  class Dec implements Runnable{
public void run(){
for(int i=0;i<100;i++){
dec();
}
 }
  }
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -